Found 10784 Articles for Python

How does issubclass() function work in Python?

Rajendra Dharmkar
Updated on 21-Feb-2020 05:13:17

114 Views

We can derive a class from multiple parent classes as follows −class A:        # define your class A ..... class B:         # define your class B ..... class C(A, B):   # subclass of A and B .....We can use issubclass() function to check the relationships of two classes and instances.For example, theissubclass(sub, sup) boolean function returns true if the given subclass sub is indeed a subclass of the superclass sup.

How does class inheritance work in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 12:43:31

166 Views

Inheritance in classesInstead of defining a class afresh, we can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.The child class inherits the attributes of its parent class, and we can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent.SyntaxDerived classes are declared much like their parent class; however, a list of base classes to inherit from is given after the class name −class SubClassName (ParentClass1[, ParentClass2, ...]):    'Optional class ... Read More

How does garbage collection work in Python?

Rajendra Dharmkar
Updated on 13-Jun-2020 13:25:47

5K+ Views

Python deletes unwanted objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically frees and reclaims blocks of memory that no longer are in use is called 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 ... Read More

How to destroy an object in Python?

Sarika Singh
Updated on 18-Aug-2022 12:39:40

19K+ Views

When an object is deleted or destroyed, a destructor is invoked. Before terminating an object, cleanup tasks like closing database connections or filehandles are completed using the destructor. The garbage collector in Python manages memory automatically. for instance, when an object is no longer relevant, it clears the memory. In Python, the destructor is entirely automatic and never called manually. In the following two scenarios, the destructor is called − When an object is no longer relevant or it goes out of scope The object's reference counter reaches zero. Using the __del__() method In Python, a destructor is ... Read More

How do we access class attributes using dot operator in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 12:30:35

524 Views

A class attribute is an attribute of the class rather than an attribute of an instance of the class.In the code below class_var is a class attribute, and i_var is an instance attribute: All instances of the class have access to class_var, which can also be accessed as a property of the class itself −Exampleclass MyClass (object):     class_var = 2     def __init__(self, i_var):         self.i_var = i_var foo = MyClass(3) baz = MyClass(4) print (foo.class_var, foo.i_var) print (baz.class_var, baz.i_var)OutputThis gives the output(2, 3) (2, 4)

Python tuples are immutable then how we can add values to them?

Malhar Lathkar
Updated on 20-Feb-2020 11:29:08

172 Views

Python tuple is an immutable object. Hence any operation that tries to modify it (like append/insert) is not allowed. However, following workaround can be used.First, convert tuple to list by built-in function list(). You can always append as well as insert an item to list object. Then use another built-in function tuple() to convert this list object back to tuple.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> L1=list(T1) >>> L1 [10, 50, 20, 9, 40, 25, 60, 30, 1, 56] >>> L1.append(100) >>> L1.insert(4,45) >>> T1=tuple(L1) >>> T1 (10, 50, 20, 9, 45, 40, 25, 60, 30, 1, 56, 100)

How to append elements in Python tuple?

Sarika Singh
Updated on 22-Aug-2023 01:47:41

121K+ Views

Tuples in Python are immutable, meaning that once they are created, their contents cannot be changed. However, there are situations when we want to change the existing tuple, in which case we must make a new tuple using only the changed elements from the original tuple. Following is the example of the tuple − s = (4, 5, 6) print(s) print(type(s)) Following is the output of the above code − (4, 5, 6) Tuple is immutable, although you can use the + operator to concatenate several tuples. The old object is still present at this point, and ... Read More

How can I do Python Tuple Slicing?

Malhar Lathkar
Updated on 20-Feb-2020 11:27:57

299 Views

Slicing operator can be used with any sequence data type, including Tuple. Slicing means separating a part of a sequence, here a tuple. The symbol used for slicing is ‘:’. The operator requires two operands. First operand is the index of starting element of slice, and second is index of last element in slice+1. Resultant slice is also a tuple.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[2:4] (20, 9)Both operands are optional. If first operand is missing, slice starts from beginning. If second operand is missing, slice goes upto end.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[6:] (60, 30, 1, 56) >>> T1[:4] (10, 50, 20, 9)

How to pop-up the first element from a Python tuple?

Pythonic
Updated on 20-Feb-2020 11:27:15

3K+ Views

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

What is the difference between __str__ and __repr__ in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

137 Views

The built-in functions repr() and str() respectively call object.__repr__(self) and object.__str__(self) methods. First function computes official representation of the object, while second returns informal representation of the object. Result of both functions is same for integer object. >>> x = 1 >>> repr(x) '1' >>> str(x) '1' However, it is not the case for string object. >>> x = "Hello" >>> repr(x) "'Hello'" >>> str(x) 'Hello' Return value of repr() of a string object can be evaluated by eval() function and results in valid string object. However, result of str() can not be evaluated. ... Read More

Advertisements