Python setattr() Function26 Sept 2024 | 2 min read Python setattr() function is used to set a value to the object's attribute. It takes three arguments an object, a string, and an arbitrary value, and returns none. It is helpful when we want to add a new attribute to an object and set a value to it. The signature of the function is given below. SignatureParametersobject: It is an object which allows its attributes to be changed. name : A name of the attribute. value : A value, set to the attribute. All the parameters are required. ReturnIt returns None to the caller function. Let's see some examples of setattr() function to understand its functionality. Python setattr() Function Example 1Here, we are adding a new attribute and setting its value by using the setattr() function. Output: 102 Sohan [email protected] Python setattr() Function Example 2If we don't want to set any value, by default, it can be set None. Output: 102 Sohan None Python setattr() Function Example 3We can reassign (reset) attribute's value even after calling setattr() function. See the example below. Output: 102 Sohan [email protected] Next TopicPython Set |
Function function rounds off the digits of a number and returns the floating-point number. Signature round(number,digits) Parameters number : The number that is to be rounded. digits : It is the number for which the given number is to be rounded, and it is an optional parameter. Return It rounds-off...
1 min read
Function function returns an enumerated object. It takes two parameters first is a sequence of elements and the second is the start index of the sequence. We can get sequence elements either through a loop or () method. The () method of the iterator returned...
3 min read
Function function is used to create a tuple object. Signature tuple(iterable) Parameters Iterable: It is a sequence, collection, or an iterator object. Return It returns a tuple. Function Example 1 The below example creates tuple using tuple(). t1 = tuple() print('t1=', t1) # creating a tuple from a list t2 = tuple([1, 6, 9]) print('t2=',...
1 min read
Function function returns the __dict__ attribute of the given object. Signature vars(object) Parameters object (optional): It is an object and can be a module, class, instance, or any other object having __dict__ attribute. Return It returns the __dict__ attribute of the given object. If any object fails to match the...
1 min read
Function The python globals() function returns the dictionary of the current global symbol table. A Symbol table is defined as a data structure which contains all the necessary information about the program. It includes variable names, methods, classes etc. Signature globals() Parameters It does not contain any parameters. Return It returns the dictionary...
1 min read
Function function returns the list of names in the current local scope. If the object on which method is called has a method named __dir__(), this method will be called and must return the list of attributes. It takes a single object type argument....
2 min read
Function The python open() function opens the file and returns a corresponding file object. Signature open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameters file: It is a path like object giving the pathname of the file to be opened. mode (optional): It specifies the mode in which the file is...
2 min read
Function The python all() function accepts an iterable object (such as list,dictionary etc.). It returns True if all items in passed iterable are true, otherwise it returns False. If the iterable object is empty, the all() function returns True. Signature all (iterable) Parameters iterable - The objects which contain...
1 min read
Function function is used to compute the powers of a number. It returns x to the power of y modulus z if a third argument(z) is present, i.e. (x, y) % z. Signature pow(x, y, z) Parameters x: It is a number, a base y: It is a number, an...
1 min read
Function function is used to get the smallest element from the collection. This function takes two arguments, first is a collection of elements and second is key and returns the smallest element from the collection. Signature min (collection[, key]) Parameters collection: It is a comma-separated list of elements. key: Specifies...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India