Rajendra Dharmkar has Published 189 Articles

How we can instantiate different python classes dynamically?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:29:21

882 Views

To instantiate the python class, we need to get the class name first. This is achieved by following codedef get_class( kls ):     parts = kls.split('.')     module = ".".join(parts[:-1])     m = __import__( module )     for comp in parts[1:]:         m ... Read More

How does Python class inherit objects?

Rajendra Dharmkar

Rajendra Dharmkar

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

198 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 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

330 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 do I enumerate functions of a Python class?

Rajendra Dharmkar

Rajendra Dharmkar

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

228 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

651 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

4K+ 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

478 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

How to deploy python modules on Heroku?

Rajendra Dharmkar

Rajendra Dharmkar

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

354 Views

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

731 Views

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

Introduction to Classes and Inheritance in Python

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 14:01:31

445 Views

Object-oriented programming creates reusable patterns of code to prevent code redundancy in projects. One way that recyclable code is created is through inheritance, when one subclass leverages code from another base class.Inheritance is when a class uses code written within another class.Classes called child classes or subclasses inherit methods and ... Read More

Previous 1 ... 7 8 9 10 11 ... 19 Next
Advertisements