Rajendra Dharmkar has Published 537 Articles

How do I correctly clean up a Python object?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:46:25

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

How to access Python objects within objects in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:44:26

The objects within objects can be accessed as followsExampleclass P:  def __init__(self):    self.w = Q() class Q:  def __init__(self):   self.list = [3,4,5]  def function(self):   self.list[2] = 7 y = P() f = [y] print f[0].w.function() print f[0].w.listOutputGives output asNone [3, 4, 7]

How to convert JSON data into a Python object?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:40:26

The following code converts a json object(string) into a python object(dictionary). We import the json module and use the json.loads() method to do this.Exampleimport json json_string = '{"name":"Sonali", "age": 21, "designation":" Software developer"}' print type (json_string) def func(strng):     a =json.loads(strng)     print type(a)     print a ... Read More

How we can convert Python objects into JSON objects?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:39:28

Suppose we have a list object a = [1,2,3]. We convert a python object into a JSON object by importing json module and using the method json.dumps() as follows.>>> a = [1,2,3] >>> import json >>> json.dumps(a) '[1, 2, 3]'The JSON object we got is '[1, 2, 3]'

How to return an object from a function in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 07:38:43

The return statement makes a python function to exit and hand back a value to its caller. The objective of functions in general is to take in inputs and return something. A return statement, once executed, immediately halts execution of a function, even if it is not the last statement ... Read More

How to insert a Python object in Mongodb?

Rajendra Dharmkar

Rajendra Dharmkar

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

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. ... Read More

How to know if an object has an attribute in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 06:57:31

We can use hasattr() function to find if a python object obj has a certain attribute or property. hasattr(obj, 'attribute'):The convention in python is that, if the property is likely to be there, simply call it and catch it with a try/except block. If the property is likely to not be ... Read More

How we can create singleton class in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 16-Jun-2020 06:53:41

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

Rajendra Dharmkar

Updated on 16-Jun-2020 06:53:00

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

Rajendra Dharmkar

Updated on 16-Jun-2020 06:52:15

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

Previous 1 ... 3 4 5 6 7 ... 54 Next
Advertisements