Programming Articles - Page 2118 of 3363

Data Hiding in Python

Sarika Singh
Updated on 18-Aug-2022 12:28:53

7K+ Views

Data hiding is also known as data encapsulation and it is the process of hiding the implementation of specific parts of the application from the user. Data hiding combines members of class thereby restricting direct access to the members of the class. Data hiding plays a major role in making an application secure and more robust Data hiding in Python Data hiding in python is a technique of preventing methods and variables of a class from being accessed directly outside of the class in which the methods and variables are initialized. Data hiding of essential member function prevents the end ... Read More

Overloading Operators in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:21:12

589 Views

Suppose you have created a Vector class to represent two-dimensional vectors, what happens when you use the plus operator to add them? Most likely Python will yell at you.You could, however, define the __add__ method in your class to perform vector addition and then the plus operator would behave as per expectation −Example Live Demo#!/usr/bin/python class Vector:    def __init__(self, a, b):       self.a = a       self.b = b    def __str__(self):       return 'Vector (%d, %d)' % (self.a, self.b)    def __add__(self, other):       return Vector(self.a + other.a, self.b + other.b) v1 = ... Read More

Base Overloading Methods in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:22:55

457 Views

Following table lists some generic functionality that you can override in your own classes −Sr.No.Method, Description & Sample Call1__init__ ( self [,args...] )Constructor (with any optional arguments)Sample Call : obj = className(args)2__del__( self )Destructor, deletes an objectSample Call : del obj3__repr__( self )Evaluable string representationSample Call : repr(obj)4__str__( self )Printable string representationSample Call : str(obj)5__cmp__ ( self, x )Object comparisonSample Call : cmp(obj, x)

Destroying Objects (Garbage Collection) in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:16:43

1K+ Views

Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection.Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or ... Read More

Built-In Class Attributes in Python

Vikram Chiluka
Updated on 22-Sep-2022 08:32:28

18K+ Views

In this article, we will explain to you the built-in class attributes in python The built-in class attributes provide us with information about the class. Using the dot (.) operator, we may access the built-in class attributes. The built-in class attributes in python are listed below − Attributes Description __dict__ Dictionary containing the class namespace __doc__ If there is a class documentation class, this returns it. Otherwise, None __name__ Class name. __module__ Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ ... Read More

Creating Instance Objects in Python

Mohd Mohtashim
Updated on 29-Aug-2023 03:34:56

38K+ Views

To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts."This would create first object of Employee class" emp1 = Employee("Zara", 2000) "This would create second object of Employee class" emp2 = Employee("Manni", 5000)You access the object's attributes using the dot (.) operator with object. Class variable would be accessed using class name as follows −emp1.displayEmployee() emp2.displayEmployee() print "Total Employee %d" % Employee.empCountExampleNow, putting all the concepts together − Live Demo#!/usr/bin/python class Employee:    'Common base class for all employees'    empCount = 0    def __init__(self, name, salary):       ... Read More

Creating Classes in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:09:31

2K+ Views

The class statement creates a new class definition. The name of the class immediately follows the keyword class followed by a colon as follows −class ClassName: 'Optional class documentation string' class_suiteThe class has a documentation string, which can be accessed via ClassName.__doc__.The class_suite consists of all the component statements defining class members, data attributes and functions.ExampleFollowing is the example of a simple Python class −class Employee:    'Common base class for all employees'    empCount = 0    def __init__(self, name, salary):       self.name = name       self.salary = salary       Employee.empCount += 1    def displayCount(self): ... Read More

OOP Terminology in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:08:18

3K+ Views

Class − A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.Data member − A class variable or instance variable that holds data associated with a class and its objects.Function overloading − The assignment of more than ... Read More

String to Integer (atoi) in C++

Arnab Chakraborty
Updated on 27-Apr-2020 11:33:17

756 Views

Suppose we have to design a module, that first discards as many whitespace characters as necessary until the first non-whitespace character is reached. After that, starting from this character, it takes an optional initial plus sign or minus sign followed by as many numerical digits, and interprets them as a numerical value.When the first sequence of non-whitespace characters in str is not a valid integral number, or when no such sequence exists because either str is empty or it contains only whitespaces, no conversion will be performed.So if the input is like “-45”, the output will be -45.To solve this, ... Read More

ZigZag Conversion in C++

Arnab Chakraborty
Updated on 27-Apr-2020 11:25:10

1K+ Views

Suppose the string is like " IWANTTOLEARNCODE". This string is written in a zigzag way on a given number of rows say n. So the pattern is looking like thisITAOWNOERCDALNEWhen we read the line like − "ITAOWNOERCDALNE"So we have to create one module that can perform this kind of operation by taking the string and the number of rows.To solve this, we will follow these stepswhen n = 1, then return screate an array of strings arr of size nrow := 0, and down := truefor i in range 0 to size of string – 1insert s[i] at the end ... Read More

Advertisements