Rajendra Dharmkar has Published 784 Answers

How we can split Python class into multiple files?

Rajendra Dharmkar
Published on 25-Jan-2018 10:40:51
To split a python program or a class into multiple files, we need to refactor and rewrite the code into two or more classes as per convenience while ensuring that the functionality of the original code is maintained. The answer to this question is as above in a generic way ... Read More

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

Rajendra Dharmkar
Published on 25-Jan-2018 10:21:31
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
Published on 25-Jan-2018 07:55:16
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
Published on 25-Jan-2018 07:30:14
__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
Published on 25-Jan-2018 06:52:08
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 get list of methods in a Python class?

Rajendra Dharmkar
Published on 24-Jan-2018 13:45:59
The following code prints the list of methods 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 ... Read More

What are static methods in a Python class?

Rajendra Dharmkar
Published on 24-Jan-2018 13:14:02
Any python class has three types of methods like the instance methods, class methods and the static methods.ExampleConsider the codeclass OurClass:     def method(self):         return 'instance method called', self      @classmethod     def classmethod(cls):         return 'class method called', cls ... Read More

What are Getters/Setters methods for Python Class?

Rajendra Dharmkar
Published on 24-Jan-2018 13:02:03
Getters and setters are used in many object oriented programming languages to ensure the principle of data encapsulation. They are known as mutator methods as well. Data encapsulation is seen as the bundling of data with the methods that operate on these data. These methods are of course the getter ... Read More

What are the advantages in using a Python class?

Rajendra Dharmkar
Published on 24-Jan-2018 12:22:10
Classes are a mechanism to organize your code into generic, reusable peices of code. At their best they are reusable code snippets that will be used over and over again with little or no modification. The class concept was inspired by biological collections of features (attributes) and abilities (methods).Functions are ... Read More

How do I correctly clean up a Python object?

Rajendra Dharmkar
Published on 24-Jan-2018 12:07:53
Cleanup happens to globals by setting them to None. The locals self destruct at the end of the session. The function __del__ called by Python sets the globals to None.Consider the following code where there is clean up of all objects in the given class −Exampleclass Counter:     Count ... Read More
Advertisements