Cara menggunakan SETATTR pada Python

Cara menggunakan SETATTR pada Python

Table of Contents

  • Introduction to Python getattr
  • Examples of Python getattr
  • Recommended Articles
  • Why Getattr is used in Python?
  • What is Getattr and Setattr in Python?
  • What is __ Getattr __?
  • How many arguments Getattr () function receives in Python?
  • How many arguments can be passed to a Python function?
  • What does Getattr () do in Python?
  • Does Getattr return methods?
  • What is Getattr and Setattr in Python?

Table of Contents

  • Introduction to Python getattr
  • Examples of Python getattr
  • Recommended Articles
  • Why Getattr is used in Python?
  • What is Getattr and Setattr in Python?
  • What is __ Getattr __?
  • How many arguments Getattr () function receives in Python?

Introduction to Python getattr

Python getattr is used to get an object attribute value, and if no attribute of that object is identified, the default value is returned. The getattr function takes two parameters, the name of the object and the name of the member data. This particular attribute works the same as we do use dot character. For ex: Name. We only need to pass the name of the object and the name of that particular object variable, and it will return the value of that particular object variable.

Syntax:

getattr(object, member_data[, default_parameter])

The third parameter in the getattr method is optional, but it is good to add the third parameter to prevent hard traceback error in python. If the member data-name passed in the getattr doesn’t exist, then python will return an error; to prevent this error, we pass the third parameter.

Examples of Python getattr

Following are the different examples of python getattr.

Example #1

Code:

class Car:
    name = "Audi A3";
    Price = "13500"
print(getattr(Car,'name'))

Output:

Explanation: In the above example, we have created a class Car and added two attribute names and prices with their values. Now we will get the values of the attribute without creating an object using the getattr method. We will pass the class name as the first parameter and the attribute name as the second parameter, and it returns the attribute value of the class. In this example, we are not using any third parameter, as it is optional. The second parameter i.e. is the attribute name, should be a string.

Example #2

Code:

class Car:
    name = "Audi A3";
    Price = "13500"
print(getattr(Car,'namee'))

Output:

Explanation: In this example, we are trying to pass the attribute name of member data that doesn’t exist in the class, and now python will return the error, as their no such attribute in this car.

Example #3

Code:

class Car:
    name = "Audi A3";
    Price = "13500"
print(getattr(Car,'namee',0))

Output:

Explanation: In this example, we have defined the third parameter 0 to prevent any kind of error. Now we are passing the attribute name that doesn’t exist, but this time we get the default value instead of the error.

Example #4

Code:

class Car:
    name = "Audi A3";
    Price = "13500"    
print(Car.name)

Output:

Explanation: In the above example, we are using the same problem, but this time, we access the attribute value using the class object using a dot character. It is also returning the same result, but in case if the attribute name doesn’t exist, there is no way to prevent the error, to prevent such an error, we make use of the getattr method and pass any default value.

Example #5

Code:

class Car:
    name = "Audi A3";
    Price = "13500"
    @staticmethod
    def Truck():
        return "This is new truck"
print(getattr(Car,'Truck')())

Output:

Explanation: In the above example, we are using a function inside our car class. We can also execute our function Truck using the getattr function. We need to pass the class name and function name in single quotes and then round the bracket in the last. Round bracket lets you know the getattr function to search for function instead of attribute.

Conclusion

Python getattr method is a very useful feature when we have a class and too many attributes, and we are not sure about attribute name. If we go by the normal method using dot character, then there are many chances of getting an error if the attribute name is not found, so we use the getattr function to overcome that error by a default value.

Why Getattr is used in Python?

Python getattr() function is used to get the value of an object's attribute and if no attribute of that object is found, default value is returned. Basically, returning the default value is the main reason why you may need to use Python getattr() function.

What is Getattr and Setattr in Python?

Python setattr() and getattr() goes hand-in-hand. As we have already seen what getattr() does; The setattr() function is used to assign a new value to an object/instance attribute. Syntax. Use a different Browser.

What is __ Getattr __?

__getattr__ Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self).

How many arguments Getattr () function receives in Python?

getattr() is one of the coolest built-in functions in Python. It takes three arguments: an object. name of the object's attribute but in the string format.

How many arguments can be passed to a Python function?

In Python, we can define two types of parameters that have variable lengths. They can be considered special types of optional parameters. In other words, there is no limit to the number of arguments that are passed to the parameter when we call this function.

What does Getattr () do in Python?

The getattr() function returns the value of the specified attribute from the specified object.

Does Getattr return methods?

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.

What is Getattr and Setattr in Python?

Python setattr() and getattr() goes hand-in-hand. As we have already seen what getattr() does; The setattr() function is used to assign a new value to an object/instance attribute. Syntax. Use a different Browser.