简体   繁体   English

属性定义

[英]Attribute definition

I am currently writing a paper on python fundamentals and I need some help on it as I could not find a single consistent definition for Attribute我目前正在写一篇关于 Python 基础知识的论文,我需要一些帮助,因为我找不到 Attribute 的单一一致定义

class Person:
    def __init__(self,weight):
        self.weight = weight

An attribute is something that describes an object for example weight = 75. In this case, what is an attribute exactly is it 75 or is it weight(the variable) or is the combination of both the variable and the value.属性是描述对象的东西,例如重量 = 75。在这种情况下,属性究竟是 75 还是重量(变量),或者是变量和值的组合。

Some people say attribute is the data stored in the object, which is correct if we consider 75 to be an attribute.有人说属性是存储在对象中的数据,如果我们认为75是一个属性,这是正确的。

Some people call attribute to be instance and class variables that store data pertaining to the class and the instance, which is correct if we consider weight to be an attribute(the variable name)有些人将属性称为实例和类变量,用于存储与类和实例有关的数据,如果我们将权重视为属性(变量名称),这是正确的

Some call attribute the combination of name and a value which means weight = 75 as a whole becomes the attribute有些人称属性为名称和值的组合,这意味着 weight = 75 作为一个整体成为属性

All the above definitions are slightly different if we go by their literal meaning and people use them interchangeably如果我们按字面意思理解并且人们可以互换使用它们,那么上述所有定义都略有不同

Am I missing something??我错过了什么吗??

I just need to be sure before I send the draft to my HOD我只需要在将草稿发送给我的 HOD 之前确定

Thank you, everyone谢谢大家

Please do take good care of yourself and avoid traveling as much as possible Chyanit请照顾好自己,尽量避免旅行 Chyanit

TLDR: The attribute is the value produced by looking up the attribute name. TLDR:属性是通过查找属性名称产生的值。


According to the Python Language Reference, Attribute references , in the statement根据Python Language Reference 中的 Attribute 引用,在声明中

value = obj.name

obj.name is an attribute reference, name is the attribute name, and the produced value is the attribute. obj.name是属性引用, name是属性名,产生的value是属性。 Note that value is the attribute in this case because it was produced by the attribute reference, not because it is inherently related.请注意,在这种情况下value是属性因为它是由属性引用生成的,而不是因为它具有内在的相关性。 For example, an attribute reference may produce a new value every time it is evaluated.例如,属性引用可能会在每次评估时产生一个新value

The primary must evaluate to an object of a type that supports attribute references, which most objects do.主要对象必须评估为支持属性引用的类型的对象,大多数对象都这样做。 This object is then asked to produce the attribute whose name is the identifier .然后要求该对象生成名称为标识符的属性 This production can be customized by overriding the __getattr__() method.可以通过覆盖__getattr__()方法来自定义此生产。 If this attribute is not available, the exception AttributeError is raised.如果此属性不可用,则会引发异常 AttributeError。 Otherwise, the type and value of the object produced is determined by the object.否则,产生的对象的类型和值由对象决定。 Multiple evaluations of the same attribute reference may yield different objects.对同一属性引用的多次评估可能会产生不同的对象。

In an attribute assignment such as在属性赋值中,例如

obj.name = value

the value does not necessarily become the attribute.value并不一定成为属性。 If the attribute name points to a data descriptor , value may be stored, modified or discarded.如果属性名称指向数据描述符,则value可能会被存储、修改或丢弃。

First let's talk about general objects in Python.首先让我们谈谈Python中的一般对象。 The documentation defines 3 characteristics for every object:文档为每个对象定义了 3 个特征:

  • identity身份

    An object's identity never changes once it has been created;一个对象的身份一旦被创建就永远不会改变; you may think of it as the object's address in memory.您可以将其视为对象在内存中的地址。

  • type类型

    An object's type determines the operations that the object supports (eg, “does it have a length?”) and also defines the possible values for objects of that type.对象的类型决定了该对象支持的操作(例如,“它有长度吗?”)并且还定义了该类型对象的可能值。 The type() function returns an object's type (which is an object itself). type() 函数返回一个对象的类型(它是一个对象本身)。 Like its identity, an object's type is also unchangeable.与其身份一样,对象的类型也是不可更改的。

  • value价值

    The value of some objects can change.某些对象的值可能会发生变化。 Objects whose value can change are said to be mutable;值可以改变的对象被称为可变的; objects whose value is unchangeable once they are created are called immutable.其值一旦创建就不可更改的对象称为不可变的。

Understanding objects means understanding types, but types are objects also.理解对象意味着理解类型,但类型也是对象。 These TypeObjects are one of the most important structures in Python and define the basic functionality of an object.这些TypeObject是 Python 中最重要的结构之一,定义了对象的基本功能。 Specifically they implement the getattr and setattr functions.具体来说,它们实现了 getattr 和 setattr 函数。

The default behavior for this implementation is described here : 此处描述了此实现的默认行为:

The default behavior for attribute access is to get, set, or delete the attribute from an object's dictionary.属性访问的默认行为是从对象的字典中获取、设置或删除属性。 For instance, ax has a lookup chain starting with a.__dict__['x'] , then type(a).__dict__['x'] , and continuing through the base classes of type(a) excluding metaclasses.例如, ax有一个以a.__dict__['x']开始的查找链,然后是type(a).__dict__['x'] ,并继续遍历type(a)的基类,不包括元类。 If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead.如果查找的值是定义描述符方法之一的对象,则 Python 可能会覆盖默认行为并改为调用描述符方法。 [...] [...]

the methods for an class or an instance which can overwrite the default behaviour are __get__ , __set__ and __delete__ .可以覆盖默认行为的类或实例的方法是__get____set____delete__

The documentation for the __get__ method specifies: __get__方法的文档指定:

This method should return the computed attribute value or raise an AttributeError exception.此方法应返回计算出的属性值或引发 AttributeError 异常。

Summarizing all the above, my formal definition for an attribute would be along the lines of:总结以上所有内容,对属性的正式定义如下:

  • attributes are objects with identity, type and value属性是具有身份、类型和值的对象
  • they are owned by another object它们归另一个对象所有

Further reading:进一步阅读:

https://docs.python.org/3/reference/datamodel.html https://docs.python.org/3/reference/datamodel.html

https://docs.python.org/3/c-api/typeobj.html https://docs.python.org/3/c-api/typeobj.html

https://docs.python.org/3/howto/descriptor.html?highlight=descriptor https://docs.python.org/3/howto/descriptor.html?highlight=descriptor

https://realpython.com/python-descriptors/ https://realpython.com/python-descriptors/

Basically, an attribute is characteristic of an entity/object.基本上,属性是实体/对象的特征。 It describes the qualities of an object.它描述了物体的品质。

For example, if you have a Employee class having employee_id as X1234, this employee_id is a characteristic of the employee, which can be used to identify an employee.例如,如果您有一个Employee 类,其employee_id 为X1234,则这个employee_id 是雇员的特征,可用于识别雇员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM