How to explain an attribute in Python to a novice

后端 未结 3 927
甜味超标
甜味超标 2021-01-27 04:19

Can anyone explain in simple words what an attribute in Python language is?

For instance what can I say about

list.append(x)

which adds

3条回答
  •  执笔经年
    2021-01-27 04:39

    Properties are just one thing that can be modeled using attributes. As you say, the operation of adding to the end of a list is another such thing. This has a rather straightforward interpretation in Python; since functions are first-class values, they can be stored as the value of an attribute like any other type. Here, list.append is simply a function, which when called adds its argument to the end of list. You can see that it doesn't matter by what name you call the function. The following are identical in behavior:

    1. list.append(5)
    2. f = list.append; f(5)

    In both cases, the same bound method is called with an argument of 5.

提交回复
热议问题