数据属性和方法属性之间的差异 [英] Differences between data attributes and method attributes

查看:83
本文介绍了数据属性和方法属性之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是方法属性和数据属性?它们之间的区别是什么?

What is a method attribute, and a data attribute? What the difference between them and what they have in common?

我正在阅读python 2.7.9( https://docs. python.org/2/tutorial/classes.html#random-remarks ),两者突然变得难以理解. 我会欣赏它的一些光亮.

I was reading python 2.7.9 (https://docs.python.org/2/tutorial/classes.html#random-remarks) and suddenly both became hard to understand. I'll appreciate some light over it.

推荐答案

属性是使用点语法obj.attribute在另一个对象上查找的变量. Python的设计方式,属性查找可以完成各种事情,并且如果您不真正了解正在发生的事情(这是链接到的文档所警告的内容),那么有时这种多样性可能会导致错误.

An attribute is a variable that is looked up on another object using dot syntax: obj.attribute. The way Python is designed, attribute lookups can do a variety of things, and that variety can sometimes lead to bugs if you don't really understand what is happening (this is what the documentation you linked to warns about).

最基本的问题是,属性查找可以找到存储在对象实例字典中的值,或者可以从对象的类(或基类,如果正在进行继承)中找到内容.方法是存储在类中的函数,但是您通常通过在实例上查找它们来使用它们(绑定"该方法,并在调用该方法时将对象作为第一个争论对象插入).

The most basic issue is that an attribute lookup can find either a value stored in the object's instance dictionary, or it can find something from the object's class (or a base class, if there's inheritance going on). Methods are functions stored in the class, but you usually use them by looking them up on an instance (which "binds" the method, inserting the object as the first arguemnt when the method is called).

什么时候检查的确切顺序有点复杂(我在

The exact sequence of what is checked when is a bit complicated (I described the full process in an answer to another question), but at the most basic level, instance attributes usually take precedence over class attribute.

如果同时存在一个实例属性和一个具有相同名称的类属性,则通常只能访问该实例属性.如果是意想不到的话,这可能会非常令人困惑.

If an instance attribute and a class attribute with the same name both exist, usually only the instance attribute will be accessible. This can be very confusing if it is unintended.

考虑以下代码:

class Foo(object):
    def __init__(self, lst):
        self.lst = lst

    def sum(self):
        self.sum = sum(self.lst)
        return self.sum

f = Foo([1,2,3])

print(f.sum())
print(f.sum())

在此代码的底部,我们进行了两个相同的调用.第一个很好用,但是第二个会引发异常.

At the bottom of this code, we make two identical calls. The first works just fine, but the second will raise an exception.

这是因为第一次查找f.sum时,我们在Foo类中找到了一个方法.我们可以毫无问题地调用该方法.问题来自于sum方法将其计算结果(self.lst中元素的总和)分配给也称为sum的实例属性的事实.这会从视图中隐藏sum方法.

This is because the first time we look up f.sum we find a method in the Foo class. We can call the method with no problems. The trouble comes from the fact that the sum method assigns the result of its calculation (the sum of the elements in self.lst) to an instance attribute also named sum. This hides the sum method from view.

当第二个f.sum()调用查找f.sum时,它将找到包含整数6的实例属性,而不是预期的方法.整数不可调用,因此会出现异常.

When second f.sum() call looks up f.sum, it finds the instance attribute, containing the integer 6, rather than the expected method. An integer is not callable, so we get an exception.

当然,解决方案是对方法和属性使用不同的名称.上面的代码是一个非常简单的示例.这类事情在更复杂的代码中引起的错误可能很难找出.

The solution, of course, is not to use the same name for the method and attribute. The code above is a pretty trivial example. The bugs caused by this sort of thing in more complex code can be much more difficult to figure out.

如果您正在编写将属性添加到您不太了解的对象的代码,则应避免使用通用名称.如果要编写mixin类,请考虑在属性名称中使用两个前导下划线来触发Python的名称修饰,这正是针对这种情况而设计的.

If you're writing code that adds attributes to objects you don't know much about, you should be careful to avoid common names. If you're writing a mixin class, consider using two leading underscores in the attribute names to trigger Python's name mangling, which is designed for exactly this sort of situation.

这篇关于数据属性和方法属性之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆