Server Side Programming Articles - Page 2583 of 2650

What is difference in getattr() and setattr() functions in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 10:39:57

471 Views

The getattr() methodThe 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 the given objectdefault, if no named attribute is foundAttributeError exception, if named attribute is not found and default is not definedThe setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() ... Read More

What does delattr() function do in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 10:34:47

83 Views

Python delattr()The delattr() deletes an attribute from the object if the object allows it.SyntaxThe syntax of delattr() is −delattr(object, name)The delattr() method takes two parameters −The delattr() doesn't return any value (returns None). It only removes an attribute (if object allows it).Exampleclass Coordinate:   x = 12   y = -7   z = 0 point1 = Coordinate() print('x = ', point1.x) print('y = ', point1.y) print('z = ', point1.z) delattr(Coordinate, 'z') print('--After deleting z attribute--') print('x = ', point1.x) print('y = ', point1.y) # Raises Error ... Read More

What does setattr() function do in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 10:32:32

138 Views

The setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() method returns None.Exampleclass Male:     name = 'Abel' x = Male() print('Before modification:', x.name) # setting name to 'Jason' setattr(x, 'name', 'Jason') print('After modification:', x.name)OutputThis gives the output('Before modification:', 'Abel') ('After modification:', 'Jason')

What does hasattr() function do in Python?

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

313 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 two parameters −The hasattr() method returns −True, if object has the given named attributeFalse, if object has no given named attributeExampleclass Male:     age = 21     name = 'x' x = Male() print('Male has age?:', hasattr(x, 'age')) print('Male has salary?:', hasattr(x, 'salary'))OutputThis gives the output('Male has age?:', ... Read More

What does getattr() function do in Python?

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

425 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 the given objectdefault, if no named attribute is foundAttributeError exception, if named attribute is not found and default is not definedExampleclass Male:     age = 21     name = "Abel" x = Male() print('The age is:', getattr(x, "age")) print('The age is:', x.age)OutputThis gives the output('The age is:', 21) ... Read More

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

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 __init__(self):         self.object_elem = 789 c1 = MyClass() c2 = MyClass() # Initial values of both elements >>> print c1.stat_elem, c1.object_elem 456 789 >>> print c2.stat_elem, c2.object_elem 456 789 # Let's try changing the static element MyClass.static_elem = 888 >>> print c1.stat_elem, c1.object_elem 888 789 >>> print ... Read More

What is the correct way to define class variables in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 10:19:04

225 Views

Class variables are variables that are declared outside the__init__method. These are static elements, meaning, they belong to the class rather than to the class instances. These class variables are shared by all instances of that class. Example code for class variables Exampleclass MyClass:   __item1 = 123   __item2 = "abc"   def __init__(self):     #pass or something elseYou'll understand more clearly with more code −class MyClass:     stat_elem = 456     def __init__(self):         self.object_elem = 789 c1 = MyClass() c2 = MyClass() # Initial values of both elements >>> print c1.stat_elem, c1.object_elem 456 ... Read More

How to declare an attribute in Python without a value?

Vikram Chiluka
Updated on 22-Sep-2022 08:53:00

5K+ Views

In this article, we will show you how to declare an attribute in python without a value. In Python, as well as in several other languages, there is a value that means "no value". In Python, that value with no value is None, let us see how it is used. You simply cannot. Variables are just names in Python. A name always refers to an object ("is bound"). It is conventional to set names that do not yet have a meaningful value but should be present to None. Method 1: By Initializing them with None Directly We can directly assign ... Read More

What is difference between self and __init__ methods in python Class?

Rajendra Dharmkar
Updated on 13-Jun-2020 08:30:30

8K+ Views

selfThe word 'self' is used to represent the instance of a class. By using the "self" keyword we access the attributes and methods of the class in python.__init__ method"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.ExampleFind out the cost of a rectangular field with breadth(b=120), length(l=160). It costs x (2000) rupees per 1 square unitclass Rectangle:    def __init__(self, length, breadth, unit_cost=0):        self.length ... Read More

How to create instance Objects using __init__ in Python?

Rajendra Dharmkar
Updated on 13-Jun-2020 08:29:40

861 Views

The instantiation or calling-a-class-object operation creates an empty object. Many classes like to create objects with instances with a specific initial state. Therefore a class may define a special method named __init__(), as follows −def __init__(self) −    self.data = [ ]When a class defines an __init__() method, class instantiation automatically invokes the newly-created class instance which is obtained by −x = MyClass()The __init__() method may have arguments. In such a case, arguments given to the class instantiation operator are passed on to __init__(). For example, >>> class Complex: ...     def __init__(self, realpart, imagpart): ...       ... Read More

Advertisements