Rajendra Dharmkar has Published 452 Articles

Do you think declarations within Python class are equivalent to those within __init__ method?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:19:01

73 Views

Declarations anywhere in the class(other than in __init__) and declarations in the __init__method are not the same. The following code shows this to be true.Exampleimport sys class foo():     print 'within class'     def __init__(self):         print 'within init'     def do_smthng(self):     ... Read More

How does Python class inherit objects?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:06:50

149 Views

In Python 2.x there are two styles of classes depending on the presence or absence of a built-in type as a base-class −‘Old style’ or "Classic" style classes: they have no built-in type as a base class −>>> class OldFoo:      # no base class ...     pass ... Read More

How does constructor method __init__ work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:02:16

1K+ Views

__init__ "__init__" is a reserved method in python classes. It is known as a constructor in OOP concepts. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.How can we use  "__init__ " ?Let's consider that we are ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

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

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

How to insert a Python object in Mongodb?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:36:08

576 Views

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. ... Read More

How do we reference Python class attributes?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 06:17:38

564 Views

From the Python documentation −Class objects support two kinds of operations: attribute references and instantiation.Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the ... Read More

How do I enumerate functions of a Python class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 11:58:49

160 Views

The following code prints the list of functions of the given class as followsExampleclass foo:     def __init__(self):         self.x = x     def bar(self):         pass     def baz(self):         pass print (type(foo)) import inspect print(inspect.getmembers(foo, predicate=inspect.ismethod))OutputThe output is  [('__init__', ), ('bar', ), ('baz', )]   

What is the difference between old style and new style classes in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 11:41:27

471 Views

In Python 2.x there's two styles of classes depending on the presence or absence of a built-in type as a base-class −"classic" style or old style classes have no built-in type as a base class: >>> class OldSpam:      # no base class ...     pass >>> OldSpam.__bases__ ... Read More

How data hiding works in Python Classes?

Rajendra Dharmkar

Rajendra Dharmkar

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

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

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

Rajendra Dharmkar

Rajendra Dharmkar

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

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

Advertisements