Rajendra Dharmkar has Published 547 Articles

What's the best place for python classes in a Django project?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:36:33

You can put said class where you want.exampleproject   foo      bar.py      baz.py      qux.pyThen you can import your qux.py module by using import.from project.foo.qux import *Making directory is better when you are dealing with a large number of files. For example −project     foo ... Read More

How to serialize a Python class?

Rajendra Dharmkar

Rajendra Dharmkar

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

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 ... Read More

How to convert a string to a Python class object?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:35:03

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 ... Read More

How we can instantiate different python classes dynamically?

Rajendra Dharmkar

Rajendra Dharmkar

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

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

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 08:19:01

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

Rajendra Dharmkar

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

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

Rajendra Dharmkar

Updated on 16-Jun-2020 08:02:16

__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

Rajendra Dharmkar

Updated on 16-Jun-2020 07:54:05

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

Rajendra Dharmkar

Updated on 16-Jun-2020 07:50:27

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 output is  [('__init__', ), ('bar', ), ('baz', )]

What are Getters/Setters methods for Python Class?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:48:08

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

Previous 1 ... 5 6 7 8 9 ... 55 Next
Advertisements