
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 3254 of 3366

3K+ Views
Python code is organized in files called "modules" and groups of related modules called “packages".A module is a distinct unit that may have one or more closely-related classes. Modules need to be imported before they are read, used, maintained and extended if needed. So a module is a unit or reuse.The rule is this: a module is the unit of reuse. Everything in Python libraries and other Python applications is either a module or a package of modules.There is no limit on how many classes one can put in a file or a module. It all depends on how big ... Read More

3K+ Views
Using comparison operators, we may compare various data types in Python. When creating custom classes, we are unable to simply compare them using the comparison operators. This article will go over various approaches to verify equivalence ("equality") in Python classes. Equality of class objects The == operator makes it simple to determine whether two built-in objects, such as strings or integers, are equal. This is demonstrated in the example below. Example Following is an example of == operator − char1 = 365 char2 = 83 result = char1 == char2 print("{} and {} are equivalent to each other:{}".format(char1, char2, result)) ... Read More

665 Views
In Python 2.x there's two styles of classes depending on the presence or absence of a built-in type as a base-class −"classic" style or old style classes have no built-in type as a base class: >>> class OldSpam: # no base class ... pass >>> OldSpam.__bases__ ()"New" style classes: they have a built-in type as a base class meaning that, directly or indirectly, they have object as a base class −>>> class NewSpam(object): # directly inherit from object ... pass >>> NewSpam.__bases__ (, ) >>> class IntSpam(int): ... Read More

206 Views
In Python 2.x there are two styles of classes depending on the presence or absence of a built-in type as a base-class −‘Old style’ or "Classic" style classes: they have no built-in type as a base class −>>> class OldFoo: # no base class ... pass >>> OldFoo.__bases__ ()"New" style classes: they have a built-in type as a base class meaning that, directly or indirectly, they have object as a base class −>>> class NewFoo(object): # directly inherit from object ... pass >>> NewFoo.__bases__ (, )In Python 3.x however, only ... Read More

3K+ Views
Metaprogramming in python is defined as the ability of a program to influence itself. It is achieved by using metaclass in python. Metaclasses in Python Metaclasses are an OOP concept present in all python code by default. Python provides the functionality to create custom metaclasses by using the keyword type. Type is a metaclass whose instances are classes. Any class created in python is an instance of type metaclass. The type() function can create classes dynamically as calling type() creates a new instance of type metaclass. Syntax Syntax to create a class using type() is given below − class ... Read More

5K+ Views
Python variable name may begin with a single underscore. It functions as a convention to indicate that the variable name is now a private variable. It should be viewed as an implementation detail that could change at any time. Programmers can assume that variables marked with a single underscore are reserved for internal usage. Single underscores are advised for semi-private variables, and double underscores are advised for fully private variables. To paraphrase PEP-8; single leading underscore: a poor signal of "internal use." For instance, from M import * excludes objects with names that begin with an underscore. Syntax The syntax ... Read More

5K+ Views
When a double underscore is added as prefix to python variables the name mangling process is applied to a specific identifier(__var) In order to avoid naming conflicts with the subclasses, name mangling includes rewriting the attribute name. Example Following is the program to explain the double underscore in Python − class Python: def __init__(self): self.car = 5 self._buzz = 9 self.__fee = 2 d = Python() print(dir(d)) Output Following is an output of the above code − ['_Python__fee', ... Read More

4K+ Views
Data hidingIn Python, we use double underscore before the attributes name to make them inaccessible/private or to hide them.The following code shows how the variable __hiddenVar is hidden.Exampleclass MyClass: __hiddenVar = 0 def add(self, increment): self.__hiddenVar += increment print (self.__hiddenVar) myObject = MyClass() myObject.add(3) myObject.add (8) print (myObject.__hiddenVar)Output 3 Traceback (most recent call last): 11 File "C:/Users/TutorialsPoint1/~_1.py", line 12, in print (myObject.__hiddenVar) AttributeError: MyClass instance has no attribute '__hiddenVar'In the above program, we tried to access hidden variable outside the class using object and it threw an ... Read More

494 Views
The cmp() functionThe cmp(x, y) function compares the values of two arguments x and y −cmp(x, y)The return value is −A negative number if x is less than y.Zero if x is equal to y.A positive number if x is greater than y.The built-in cmp() function will typically return only the values -1, 0, or 1. However, there are other places that expect functions with the same calling sequence, and those functions may return other values. It is best to observe only the sign of the result.>>> cmp(2, 8) -1 >>> cmp(6, 6) 0 >>> cmp(4, 1) 1 >>> cmp('stackexchange', ... Read More

645 Views
In Python, the __str__() method is used to compute the informal or human readable string representation of an object. This method is called by the built-in print(), string and format() functions. This method is called by the implementation of the default __format__() method and the built-in function print(). This method returns the string object. In a class, if the __str__() method is not defined, Python will fall back to using the __repr__() method. If neither of these dunder methods is defined, the class will return the default string representation of the object Example Here, we have created a class named ... Read More