Rajendra Dharmkar has Published 189 Articles

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

Rajendra Dharmkar

Rajendra Dharmkar

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

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

How does garbage collection work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

6K+ 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 ... Read More

What does hasattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:44:28

285 Views

The hasattr() method in PythonThe hasattr() method returns true if an object has the given named attribute and false if it does not.SyntaxThe syntax of hasattr() method is −hasattr(object, name)The hasattr() is called by getattr() to check to see if AttributeError is to be raised or not.The hasattr() method takes ... Read More

What does getattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:43:32

405 Views

Python getattr()The 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 of ... Read More

Explain the variables inside and outside of a class __init__() function in Python.

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:42:06

1K+ Views

Class variables vs Instance VariablesAll variables outside the class __init__ function in Python are class variables while those inside the same are instance variables. The difference between the class variables and instance variables is understood better by examining the code belowExampleclass MyClass:     stat_elem = 456     def ... Read More

What is difference between self and __init__ methods in python Class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:30:30

8K+ Views

selfThe word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.__init__ method"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called ... Read More

How to create instance Objects using __init__ in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:29:40

851 Views

The instantiation or calling-a-class-object operation creates an empty object. Many classes like to create objects with instances with a specific initial state. Therefore a class may define a special method named __init__(), as follows −def __init__(self) −    self.data = [ ]When a class defines an __init__() method, class instantiation ... Read More

How do you validate a URL with a regular expression in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 07:28:42

622 Views

There's no validate method as almost anything is a valid URL. There are some punctuation rules for splitting it up. Without any punctuation, you still have a valid URL.Depending on the situation, we use following methods.If you trust the data, and just want to verify if the protocol is HTTP, ... Read More

What does “?:” mean in a Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:20:09

2K+ Views

Non capturing groupsIf  we do not want a group to capture its match, we can write this regular expression as Set(?:Value). The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group. The regex Set(Value)? matches Set or SetValue. In the first case, the ... Read More

What is the difference between Python functions datetime.now() and datetime.today()?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:04:46

2K+ Views

The function datetime.now() takes tzinfo as keyword argument but datetime.today() does not take any keyword arguments. Quoting the docs −datetime.now() returns the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten ... Read More

Advertisements