Rajendra Dharmkar has Published 189 Articles

How to measure elapsed time in python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 07-Jun-2020 17:37:24

2K+ Views

To measure time elapsed during program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes. exampleimport time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1) # CPU seconds elapsed (floating point)OutputThis will give the output −Time ... Read More

How does isinstance() function work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

221 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 ... Read More

How does issubclass() function work in Python?

Rajendra Dharmkar

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 ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

345 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 ... Read More

How does class inheritance work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

174 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 ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

694 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 ... Read More

What is difference in getattr() and setattr() functions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:39:57

451 Views

The getattr() methodThe getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.SyntaxThe syntax of getattr() method is −getattr(object, name[, default])The getattr() method can take multiple parameters −The getattr() method returns −value of the named attribute ... Read More

What does delattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:34:47

83 Views

Python delattr()The delattr() deletes an attribute from the object if the object allows it.SyntaxThe syntax of delattr() is −delattr(object, name)The delattr() method takes two parameters −The delattr() doesn't return any value (returns None). It only removes an attribute (if object allows it).Exampleclass Coordinate:   x = 12   ... Read More

What does setattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:32:32

138 Views

The setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() method returns None.Exampleclass Male:     name = 'Abel' x = Male() print('Before modification:', x.name) # setting name to 'Jason' setattr(x, ... Read More

What is the correct way to define class variables in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:19:04

206 Views

Class variables are variables that are declared outside the__init__method. These are static elements, meaning, they belong to the class rather than to the class instances. These class variables are shared by all instances of that class. Example code for class variables Exampleclass MyClass:   __item1 = 123   __item2 = "abc" ... Read More

Advertisements