Found 34486 Articles for Programming

How do we check if a class is a subclass of the given super class in Python?

Rajendra Dharmkar
Updated on 13-Jun-2020 13:52:45

264 Views

We have the classes A and B defined as follows −class A(object): pass class B(A): passB can be proved to be a sub class of A 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 BThis gives the outputTrue (,)

How does isinstance() function work in Python?

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

124 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 isinstance() function to check the relationships of two classes and instances.Theisinstance(obj, Class) boolean function returns true if obj is an instance of class Class or is an instance of a subclass of Class

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)

C++ 'a.out' not recognised as a command

Pythonista
Updated on 10-Feb-2020 10:47:17

5K+ Views

Having entered following command from linux terminal −$ g++ helloworld.cppThe a.out file should be created in the current working directory if the compilation is successful. Check if a.out is created.To execute enter following from command line −$ ./a.outIn most cases, output of your source program is displayed. However, as in your case, error message indicating a.out is not executable is appearing. See the properties of a.out and make it executable (if not already) by following command −$ chmod +x a.outYou may require sudo privilege for this. In all probability this should work. all the bestRead More

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

122K+ 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

Advertisements