Rajendra Dharmkar has Published 784 Answers

How we can create singleton class in Python?

Rajendra Dharmkar
Published on 23-Jan-2018 14:36:10
Singleton pattern provides a strategy to limit the number of the instances of the class to one. Thus the same object is always shared by different parts of the code. Singleton can be considered as a more elegant solution to global variable because actual data is hidden behind Singleton class ... Read More

How we can compress large Python files?

Rajendra Dharmkar
Published on 23-Jan-2018 14:25:52
We can use Python to compress or extract files. We use the zipfile module in Python, to extract or compress individual or multiple files at once. This process is is easy and requires very little code. We begin by importing the zipfile module and then open the ZipFile object in ... Read More

How to find out if a Python object is a string?

Rajendra Dharmkar
Published on 23-Jan-2018 14:11:38
For Python 2.xTo check if an object obj is a string type or a subclass of a string type −isinstance(obj, basestring)because both str and unicode are subclasses of basestring.To check if obj is an instance of str or any subclass of str −isinstance(obj, str)To check if obj is an instance ... Read More

How do I look inside a Python object?

Rajendra Dharmkar
Published on 23-Jan-2018 13:58:07
Python has a strong set of following built-in functions to collect information about the attributes, properties and methods of any python object −type()dir()id()getattr()hasattr()globals()locals()callable()type() and dir() are particularly useful for finding the type of an object and its set of attributes, respectively.Read More

Explain Python class method chaining

Rajendra Dharmkar
Published on 23-Jan-2018 10:52:45
Method ChainingMethod chaining is a technique that is used for making multiple method calls on the same object, using the object reference just once. Example −Assume we have a class Foo that has two methods, bar and baz.We create an instance of the class Foo −foo = Foo()Without method chaining, ... Read More

How do we reference Python class attributes?

Rajendra Dharmkar
Published on 23-Jan-2018 10:04:18
From the Python documentation −Class objects support two kinds of operations: attribute references and instantiation.Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the ... Read More

How to dynamically load a Python class?

Rajendra Dharmkar
Published on 23-Jan-2018 09:41:29
There's no available function that takes a fully qualified class name and returns the class. However we can define a function that has this functionality.ExampleThe following code is of such a function.def get_class( s ):     parts = s.split('.')     module = ".".join(parts[:-1])     n = __import__( ... Read More

Does Python have “private” variables in classes?

Rajendra Dharmkar
Published on 23-Jan-2018 07:44:05
There are no 'private variables' in Python classes as such. All variables and methods in Python are public by default. There is sometimes an emulation of private variables by using the double underscore __ prefix to the variable's names. This makes these variables invisible or not easily visible outside of ... Read More

How to get the class name of an instance in Python?

Rajendra Dharmkar
Published on 23-Jan-2018 07:23:56
The following code shows how to get the class name of the instance in question.Exampleclass Number:     def __init__(self, number):         self.number = number n1 = Number(1) print n1.__class__ print n1.__class__.__name__OutputThis gives the output__main__.Number Number

What are public and private variables in Python class?

Rajendra Dharmkar
Published on 22-Jan-2018 13:57:16
Public VariablesPython doesn’t restrict us from accessing any variable or calling any member method in a python program.All python variables and methods are public by default in Python. So when we want to make any variable or method public, we just do nothing. Let us see the example below −Exampleclass ... Read More
Previous 1 ... 4 5 6 7 8 9 10 ... 79 Next
Advertisements