Rajendra Dharmkar has Published 537 Articles

How do I look inside a Python object?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 06:48:05

Python has a strong set of following built-in functions to collect information about the attributes, properties and methods of any python object −type()dir()id()getattr()hasattr()globals()locals()callable()type() and dir() are particularly useful for finding the type of an object and its set of attributes, respectively.

How do we reference Python class attributes?

Rajendra Dharmkar

Rajendra Dharmkar

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

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

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

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

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

How does Overloading Operators work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 09:16:31

We know that we can use +  operator for adding numbers and at the same time to concatenate strings. This is possible because +  operator is overloaded by both int  class and str  class. The operators are basically methods defined in respective classes. Defining methods for operators is known as ... 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

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

Explain str() vs repr() functions in Python

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 09:05:56

The official Python documentation says __repr__ is used to find the “official” string representation of an object and __str__ is used to find the “informal” string representation of an object. The print statement and str() built-in function uses __str__ to display the string representation of the object while the repr() ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 09:02:09

The __str__ method__str__ is a special method, like __init__, that returns a 'informal' string representation of an object. It is useful in debugging.Consider the following code that uses the __str__ methodclass Time:     def __str__(self):         return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)When we print an object, ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 08:41:23

The official Python documentation says __repr__() is used to compute the “official” string representation of an object. The repr() built-in function uses __repr__() to display the object. __repr__()  returns a printable representation of the object, one of the ways possible to create this object.  __repr__() is more useful for developers ... Read More

Previous 1 ... 4 5 6 7 8 ... 54 Next
Advertisements