Python getattr()

In our previous tutorial we learned about python system command. In this tutorial we will be discussing about Python getattr() function.

在上一教程中,我们了解了python系统命令 。 在本教程中,我们将讨论Python getattr()函数。

Python getattr()函数 (Python getattr() function)

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.

Python getattr()函数用于获取对象属性的值,如果未找到该对象的属性,则返回默认值。

Basically, returning the default value is the main reason why you may need to use Python getattr() function. So, before starting the tutorial, lets see the basic syntax of Python’s getattr() function.

基本上,返回默认值是您可能需要使用Python getattr()函数的主要原因。 因此,在开始本教程之前,让我们看一下Python的getattr()函数的基本语法。

getattr(object_name, attribute_name[, default_value])

Python getattr()示例 (Python getattr() example)

In this section, we will learn how to access attribute values of an object using getattr() function. Suppose, we are writing a class named Student. The basic attributes of Student class are student_id and student_name. Now we will create an object of the class Student and access it’s attribute.

在本节中,我们将学习如何使用getattr()函数访问对象的属性值。 假设我们正在编写一个名为Student的类。 Student类的基本属性是student_idstudent_name 。 现在,我们将创建Student类的对象并访问其属性。

class Student:
    student_id=""
    student_name=""

    # initial constructor to set the values
    def __init__(self):
        self.student_id = "101"
        self.student_name = "Adam Lam"

student = Student()
# get attribute values by using getattr() function
print('\ngetattr : name of the student is =', getattr(student, "student_name"))

# but you could access this like this
print('traditional: name of the student is =', student.student_name)

So, the output will be like this

因此,输出将如下所示

Python getattr()默认值 (Python getattr() default value)

In this section, we will use the python getattr() default value option. If you want to access any attribute that doesn’t belong to the object, then you can use the getattr() default value option.

在本节中,我们将使用python getattr()默认值选项。 如果要访问不属于该对象的任何属性,则可以使用getattr()默认值选项。

For example, if the student_cgpa attribute is not present for the student, then will show a default value.

例如,如果学生不存在student_cgpa属性,则将显示默认值。

In the following example we will see example of default value. We will also learn what happens if the attribute is not present and we are not using the default value option.

在下面的示例中,我们将看到默认值的示例。 我们还将学习如果属性不存在并且我们没有使用默认值选项时会发生什么。

class Student:
    student_id=""
    student_name=""

    # initial constructor to set the values
    def __init__(self):
        self.student_id = "101"
        self.student_name = "Adam Lam"

student = Student()
# using default value option
print('Using default value : Cgpa of the student is =', getattr(student, "student_cgpa", 3.00))
# without using default value
try:
    print('Without default value : Cgpa of the student is =', getattr(student, "student_cgpa"))
except AttributeError:
    print("Attribute is not found :(")

So, after running the code you will get output like this

因此,运行代码后,您将获得如下输出

Using default value : Cgpa of the student is = 3.0
Attribute is not found :(

Notice that AttributeError is raised when the default value is not provided while calling getattr() function.

请注意,在调用getattr()函数时未提供默认值时会引发AttributeError

使用Python getattr()函数的原因 (Reason for using Python getattr() function)

The main reason to use python getattr() is that we can get the value by using the name of the attribute as String. So you can manually input the attribute name in your program from console.

使用python getattr()的主要原因是我们可以通过使用属性名称作为String来获取值。 因此,您可以从控制台在程序中手动输入属性名称。

Again, if an attribute is not found you can set some default value, which enables us to complete some of our incomplete data.

同样,如果找不到属性,则可以设置一些默认值,这使我们能够完成一些不完整的数据。

Also if your Student class is work in progress, then we can use getattr() function to complete other code. Once Student class has this attribute, it will automatically pick it up and not use the default value.

同样,如果您的Student类正在进行中,那么我们可以使用getattr()函数来完成其他代码。 学生班级拥有此属性后,它将自动选择它,并且不使用默认值。

So, that’s all about python getattr() function. If you have any query about this, please use the comment box below.

因此,关于python getattr()函数的全部内容。 如果对此有任何疑问,请使用下面的评论框。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/16146/python-getattr

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值