Rajendra Dharmkar has Published 452 Articles

What does the str() function do in Python Object Oriented Programming?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 09:02:09

354 Views

The __str__ method__str__ is a special method, like __init__, that returns a 'informal' string representation of an object. It is useful in debugging.Consider the following code that uses the __str__ methodclass Time:     def __str__(self):         return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)When we print an object, ... Read More

What does the repr() function do in Python Object Oriented Programming?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 08:41:23

552 Views

The official Python documentation says __repr__() is used to compute the “official” string representation of an object. The repr() built-in function uses __repr__() to display the object. __repr__()  returns a printable representation of the object, one of the ways possible to create this object.  __repr__() is more useful for developers ... Read More

How to deploy python modules on Heroku?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-Jun-2020 07:42:32

223 Views

Assuming you have set up Python 3.6, Pipenv and heroku CLI installed locally and are logged in on Heroku from the CLI using the steps mentioned here: https://devcenter.heroku.com/articles/getting-started-with-python#set-up.Your application needs to have a git repository to be deployed to heroku. You need to cd in the directory where the root ... Read More

How do I format a string using a dictionary in Python 3?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 14:27:51

555 Views

You can use dictionaries to interpolate strings. They have a syntax in which you need to provide the key in the parentheses between the % and the conversion character. For example, if you have a float stored in a key 'cost' and want to format it as '$xxxx.xx', then you'll ... Read More

Introduction to Classes and Inheritance in Python

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 14:01:31

388 Views

Object-oriented programming creates reusable patterns of code to prevent code redundancy in projects. One way that recyclable code is created is through inheritance, when one subclass leverages code from another base class.Inheritance is when a class uses code written within another class.Classes called child classes or subclasses inherit methods and ... Read More

How do we check if a class is a subclass of the given super class in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 13:52:45

258 Views

We have the classes A and B defined as follows −class A(object): pass class B(A): passB can be proved to be a sub class of A in two ways as followsclass A(object):pass class B(A):pass print issubclass(B, A) # Here we use the issubclass() method to check if B is subclass ... Read More

How does garbage collection work in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 13:25:47

5K+ Views

Python deletes unwanted objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically frees and reclaims blocks of memory that no longer are in use is called Garbage Collection.Python's garbage collector runs during program execution and is triggered when an object's reference ... Read More

What does hasattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:44:28

193 Views

The hasattr() method in PythonThe hasattr() method returns true if an object has the given named attribute and false if it does not.SyntaxThe syntax of hasattr() method is −hasattr(object, name)The hasattr() is called by getattr() to check to see if AttributeError is to be raised or not.The hasattr() method takes ... Read More

What does getattr() function do in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:43:32

273 Views

Python getattr()The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.SyntaxThe syntax of getattr() method is −getattr(object, name[, default])The getattr() method can take multiple parameters −The getattr() method returns −value of the named attribute of ... Read More

Explain the variables inside and outside of a class __init__() function in Python.

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 08:42:06

1K+ Views

Class variables vs Instance VariablesAll variables outside the class __init__ function in Python are class variables while those inside the same are instance variables. The difference between the class variables and instance variables is understood better by examining the code belowExampleclass MyClass:     stat_elem = 456     def ... Read More

Advertisements