Found 10423 Articles for Python

How data hiding works in Python Classes?

Rajendra Dharmkar
Updated on 15-Jun-2020 10:56:42

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

What does the cmp() function do in Python Object Oriented Programming?

Rajendra Dharmkar
Updated on 15-Jun-2020 09:11:48

449 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

What does the str() function do in Python Object Oriented Programming?

Akshitha Mote
Updated on 14-Apr-2025 16:23:59

563 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

What does the repr() function do in Python Object Oriented Programming?

Akshitha Mote
Updated on 14-Apr-2025 16:22:45

721 Views

In Python, the __repr__() method is a built-in function used to compute the official string representation of an object. It is used in debugging and logging purposes as it provides a detailed and clear representation of an object. This method is also known as dunder method as it begins with a double underscore in the beginning and at the end. In a class, if __str__() method and the __repr__() method is defined ,then the __str__() method is executed. Example Here, we have created a class named Methods and defined the __repr__() method. When we execute the code, the output ... Read More

How does the destructor method __del__() work in Python?

SaiKrishna Tavva
Updated on 17-Mar-2025 16:59:25

650 Views

Python programmers often need to delete directories for tasks like cleaning up temporary files. Deleting directories isn't as simple as deleting files and requires careful handling. This article explores effective methods for deleting Python directories. We'll provide step-by-step explanations and code examples, covering error handling, read-only files, and recursive removal of subdirectories. Using shutil.rmtree() for Recursive Deletion The shutil module provides the rmtree() function, which recursively deletes a directory and all its contents. This is the simplest method for deleting a directory and all its contents. Example The following code defines a function called delete_directory_with_shutil. This function takes the path ... Read More

How does constructor method __init__ work in Python?

Akshitha Mote
Updated on 14-Apr-2025 16:20:56

1K+ Views

In Python, the__init__() method is a special method within a class that gets automatically called when an object is created. It is used to initialize the attributes of the object. It is also known as a constructor. The self is always the first argument to the __init__() method that refers to the instance of the class being created, allowing access to its attributes and methods. Lets try to understand the __init__() method with an example - class Employee: def __init__(self): self.name=input("Enter employee name:") self.id=int(input("Enter id:")) ... Read More

How do I make a subclass from a super class in Python?

Sarika Singh
Updated on 23-Nov-2022 08:14:02

4K+ Views

In this article we are going to discuss how to create subclass from a super class in Python. Before proceeding further let us understand what is a class and a super class. A class is a user-defined template or prototype from which objects are made. Classes offer a way to bundle together functionality and data. The ability to create new instances of an object type is made possible by the production of a new class. Each instance of a class may have attributes connected to it to preserve its state. Class instances may also contain methods for changing their state ... Read More

Introduction to Classes and Inheritance in Python

Rajendra Dharmkar
Updated on 13-Jun-2020 14:01:31

428 Views

Object-oriented programming creates reusable patterns of code to prevent code redundancy in projects. One way that recyclable code is created is through inheritance, when one subclass leverages code from another base class.Inheritance is when a class uses code written within another class.Classes called child classes or subclasses inherit methods and variables from parent classes or base classes.Because the Child subclass is inheriting from the Parent base class, the Child class can reuse the code of Parent, allowing the programmer to use fewer lines of code and decrease redundancy.Derived classes are declared much like their parent class; however, a list of ... Read More

How I can check if class attribute was defined or derived in given class in Python?

Rajendra Dharmkar
Updated on 16-Jun-2020 07:54:05

311 Views

The code below shows the if the attribute 'foo' was defined or derived in the classes A and B.Exampleclass A:     foo = 1 class B(A):     pass print A.__dict__ #We see that the attribute foo is there in __dict__ of class A. So foo is defined in class A. print hasattr(A, 'foo') #We see that class A has the attribute but it is defined. print B.__dict__ #We see that the attribute foo is not there in __dict__ of class B. So foo is not defined in class B print hasattr(B, 'foo') #We see that class B has ... Read More

How I can check if A is superclass of B in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 12:48:09

329 Views

We have the classes A and B defined as follows −class A(object): pass class B(A): passExampleA can be proved to be a super class of B in two ways as followsclass A(object):pass class B(A):pass print issubclass(B, A) # Here we use the issubclass() method to check if B is subclass of A print B.__bases__ # Here we check the base classes or super classes of BOutputThis gives the outputTrue (,)

Advertisements