调用方法与访问属性之间的区别 [英] Difference between calling a method and accessing an attribute

查看:241
本文介绍了调用方法与访问属性之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,正在使用Python 3.3.1.

I'm very new to Python, and I'm using Python 3.3.1.

class Parent: # define parent class 
    parentAttr = 100
    age = 55

    def __init__(self): 
        print ("Calling parent constructor") 

    def setAttr(self, attr): 
        Parent.parentAttr = attr 

class Child(Parent):
    def childMethod(self):
        print ('Calling child method')

现在我要创建

c=child
c.[here every thing will appear methods and attr (age,setAttr)]

如何区分方法和属性?我的意思是,什么时候使用c.SetAtrr(Argument)c.SetAtrr=value?

How can I distinguish between methods and atrributes? I mean, when do I use c.SetAtrr(Argument), and c.SetAtrr=value?

推荐答案

方法也是属性.它们恰好是可调用的对象.

Methods are attributes too. They just happen to be callable objects.

您可以使用 callable()函数:

You can detect if an object is callable by using the callable() function:

>>> def foo(): pass
...
>>> callable(foo)
True
>>> callable(1)
False

调用方法时,先查找属性(getattr()操作),然后调用结果:

When you call a method, you look up the attribute (a getattr() operation) and then call the result:

c.setAttr(newvalue)

是两个步骤;找到属性(在这种情况下,该属性在类上查找该属性,并将其视为描述符),然后调用结果对象,即方法.

is two steps; finding the attribute (which in this case looks up the attribute on the class, and treats it as a descriptor), then calls the resulting object, a method.

分配给属性时,可以将该名称重新绑定到新值:

When you assign to an attribute, you rebind that name to a new value:

c.setAttr = 'something else'

将是setattr()操作.

如果您想拦截类实例上的获取和设置属性,则可以提供

If you wanted to intercept getting and setting attributes on instances of your class, you could provide the attribute access hooks, __getattr__, __setattr__ and __delattr__.

如果要向实例添加方法,则必须将该函数视为

If you wanted to add a method to an instance, you would have to treat the function as a descriptor object, which produces a method object:

>>> class Foo: pass
... 
>>> foo = Foo()  # instance
>>> def bar(self): pass
... 
>>> bar
<function bar at 0x10b85a320>
>>> bar.__get__(foo, Foo)
<bound method Foo.bar of <__main__.Foo instance at 0x10b85b830>>

当给定实例和类时,function.__get__()的返回值是一个绑定方法.调用该方法将调用绑定了实例的self底层函数.

The return value of function.__get__(), when given an instance and a class, is a bound method. Calling that method will call the underlying function with self bound to the instance.

说到描述符, property()函数返回一个描述符,也使得具有像属性一样表现的功能成为可能;他们可以仅针对该属性拦截getattr()setattr()delattr()操作,并将其转换为函数调用:

And speaking of descriptors, the property() function returns a descriptor too, making it possible to have functions that behave like attributes; they can intercept the getattr(), setattr() and delattr() operations for just that attribute and turn it into a function call:

>>> class Foo:
...     @property
...     def bar(self):
...         return "Hello World!"
... 
>>> foo = Foo()
>>> foo.bar
"Hello World!"

访问.bar会调用bar属性get hook,然后该钩子将调用原始的bar方法.

Accessing .bar invoked the bar property get hook, which then calls the original bar method.

在几乎所有情况下,您都不需要callable()功能;您记录了API,并提供了方法和属性,并且API的用户无需进行每个属性的测试即可了解它是否可调用,从而可以解决该问题.使用属性,您可以灵活地提供在任何情况下都是真正可调用的属性.

In almost all situations, you are not going to need the callable() function; you document your API, and provide methods and attributes and the user of your API will figure it out without testing each and every attribute to see if it is callable. With properties, you have the flexibility of providing attributes that are really callables in any case.

这篇关于调用方法与访问属性之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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