Rajendra Dharmkar has Published 784 Answers

How to create Python objects based on an XML file?

Rajendra Dharmkar
Published on 30-Jan-2018 01:57:52
UntangleWe can use untangle to create Python objects based on an XML fileuntangle is a simple library which takes an XML document and returns a Python object which mirrors the nodes and attributes in its structure.For example, an XML file like this −<?xml version="1.0"?> <root>     <child name="child1"> </root>can ... Read More

How to wrap python object in C/C++?

Rajendra Dharmkar
Published on 29-Jan-2018 13:38:29
We can use the Boost Python Library to wrap python objects in C/C++. Boost Python libraryThe Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your ... Read More

How to compress Python objects before saving to cache?

Rajendra Dharmkar
Published on 29-Jan-2018 12:37:04
We need sometimes to compress Python objects (list, dictionary, string, etc) before saving them to cache and decompress after reading from cache.Firstly we need to be sure we need to compress the objects. We should check if  the data structures/objects are too big just to fit uncompressed in the cache. ... Read More

How to use Python object in C++?

Rajendra Dharmkar
Published on 29-Jan-2018 11:56:08
Here is an example in which a simple Python object is wrapped and embedded. We are using  .c for this, c++ has similar steps −class PyClass(object):     def __init__(self):         self.data = []     def add(self, val):         self.data.append(val)     def ... Read More

How to get the return value from a function in a class in Python?

Rajendra Dharmkar
Published on 29-Jan-2018 11:43:42
The following code shows how to get return value from a function in a Python classExampleclass Score():     def __init__(self):         self.score = 0         self.num_enemies = 5         self.num_lives = 3     def setScore(self, num):     ... Read More

How to attach a C method to existing Python class?

Rajendra Dharmkar
Published on 29-Jan-2018 10:28:11
This method shows how to define a new Python class from a C extension module. The methods of the class are implemented in C, but the class can still be instantiated, subclassed, and extended from Python. The same technique with inheritance can also be used to extend an existing Python ... Read More

When are python classes and class attributes garbage collected?

Rajendra Dharmkar
Published on 29-Jan-2018 10:13:33
A class attribute exists until the last reference goes away. A global variable also exists until the last reference goes away. Neither of these are guaranteed to last the entire duration of the program.Also, a class defined at module scope is a global variable. So the class (and, by implication, ... Read More

Explain Inheritance vs Instantiation for Python classes.

Rajendra Dharmkar
Published on 29-Jan-2018 08:39:37
InheritanceBeing an Object Oriented language, Python supports inheritance, it even supports multiple inheritance. Classes can inherit from other classes. A class can inherit attributes and behaviour methods from another class, called the superclass. A class which inherits from a superclass is called a subclass, also called heir class or child ... Read More

How we can extend multiple Python classes in inheritance?

Rajendra Dharmkar
Published on 29-Jan-2018 07:54:55
As per Python documentation ‘super’ can help in extending multiple python classes in inheritance.  It returns a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same ... Read More

Would you recommend to define multiple Python classes in a single file?

Rajendra Dharmkar
Published on 29-Jan-2018 07:41:53
Python is not class-based exclusively - the basic unit of code decomposition in Python is the module. A module is a distinct thing that may have one or two dozen closely-related classes. Modules may as well contain functions along with classes. In Python there is rule of  one module=one file.In ... Read More
Advertisements