Found 27104 Articles for Server Side Programming

How do I enumerate functions of a Python class?

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

159 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', )]   

How to convert a string to a Python class object?

Rajendra Dharmkar
Updated on 09-Sep-2023 23:15:42

10K+ Views

Given a string as user input to a Python function, I'd like to get a class object out of it if there's a class with that name in the currently defined namespace.Exampleclass Foobar:     pass print eval("Foobar") print type(Foobar)Output __main__.Foobar Another way to convert a string to class object is as followsExampleimport sys class Foobar:     pass def str_to_class(str):     return getattr(sys.modules[__name__], str) print str_to_class("Foobar") print type(Foobar)Output__main__.Foobar

How I can create Python class from JSON object?

Rajendra Dharmkar
Updated on 16-Jun-2020 08:38:28

897 Views

We can use python-jsonschema-objects which is built on top of jsonschema.The python-jsonschema-objects provide an automatic class-based binding to JSON schemas for use in Python.We have a sample json schema as followsschema = '''{     "title": "Example Schema",     "type": "object",     "properties": {         "firstName": {             "type": "string"         },         "lastName": {             "type": "string"         },         "age": {             "description": "Age in years", ... Read More

How do I declare a global variable in Python class?

Rajendra Dharmkar
Updated on 09-Sep-2023 23:21:14

10K+ Views

A global variable is a variable with global scope, meaning that it is visible and accessible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global scope of the program.We declare a variable global by using the keyword global before a variable. All variables have the scope of the block, where they are declared and defined in. They can only be used after the point of their declaration.ExampleExample of global variable declarationdef f():             global s             print(s)       ... Read More

How we can extend multiple Python classes in inheritance?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

351 Views

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 as that used by  getattr() except that the type itself is skipped.In other words, a call to super returns a fake object which delegates attribute lookups to classes above you in the inheritance chain. Points to note:This does not work with old-style classes.You need to pass your own class and ... Read More

When are python classes and class attributes garbage collected?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

303 Views

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, the attribute) have the same lifetime as a global variable in that case. If no instances of the class are currently live, the class and its class attributes might be garbage-collected if their reference counts become zero.

How to serialize a Python class?

Rajendra Dharmkar
Updated on 16-Jun-2020 08:35:42

259 Views

Object serialization and deserialization are a routine aspect of any non-trivial Python program. Saving to a file, reading a configuration file, responding to an HTTP request, all involve object serialization and deserialization. Serialization and deserialization involve various schemes, formats and protocols to stream Python objects and to get them back later intact. Serialization schemes, formats or protocols you choose determine how fast your program runs and how secure it is.Lets use a dictionary of python objects to serialize. Class is also a Python object. We use a Python module named pickle and its method pickle.dumps(object).foo = dict(int_list=[3, 4, 5],  text='Hello World', ... Read More

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

Rajendra Dharmkar
Updated on 09-Sep-2023 23:21:58

3K+ Views

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 Python if you restrict yourself to one class per file (which in Python is not prohibited) you may end up with large number of small files – not easy to keep track.So depending on the scenario and convenience one can have one or more classes per file in Python.Read More

Is there any tool that can convert an XSD file to a Python class as JAXB does for Java?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

762 Views

I would recommend generateDS for converting a XSD file to a Python class . In my opinion, it is a good tool for the said purpose.It (generatS) generates the Python class with all methods (setters and getters, export to XML, import from XML). It does a good job and works very well !.

How does class variables function in multi-inheritance Python classes?

Rajendra Dharmkar
Updated on 16-Jun-2020 09:04:20

335 Views

A class can be derived from more than one base classes in Python. This is called multiple inheritance.In multiple inheritance, the features of all the base classes are inherited into the derived class. The syntax for multiple inheritance is similar to single inheritance.class Super1:     pass class Super2:     pass class MultiDerived(Super1, Super2):    passIn the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice.So, in the above example of MultiDerived class the search order is ... Read More

Advertisements