Rajendra Dharmkar has Published 504 Articles

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

How to deploy python modules on Heroku?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 07:42:32

Assuming you have set up Python 3.6, Pipenv and heroku CLI installed locally and are logged in on Heroku from the CLI using the steps mentioned here: https://devcenter.heroku.com/articles/getting-started-with-python#set-up.Your application needs to have a git repository to be deployed to heroku. You need to cd in the directory where the root ... Read More

How do I format a string using a dictionary in Python 3?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 14:27:51

You can use dictionaries to interpolate strings. They have a syntax in which you need to provide the key in the parentheses between the % and the conversion character. For example, if you have a float stored in a key 'cost' and want to format it as '$xxxx.xx', then you'll ... Read More

Previous 1 ... 6 7 8 9 10 ... 51 Next
Advertisements