简体   繁体   English

如何在python中使用@以及@property和@classmethod

[英]how to use @ in python.. and the @property and the @classmethod

this is my code: 这是我的代码:

def a():
    print 'sss'

@a()
def b():
    print 'aaa'

b()

and the Traceback is: 跟踪是:

sss
Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 8, in <module>
    @a()
TypeError: 'NoneType' object is not callable

so how to use the '@' 那么如何使用'@'

thanks 谢谢

updated 更新

class a:
    @property
    def b(x):
        print 'sss'

aa=a()
print aa.b

it print : 它打印:

sss
None

how to use @property 如何使用@property

thanks 谢谢

updated2 updated2

and the classmethod: 和类方法:

class a:
    @classmethod
    def b(x):
        print 'sss'

aa=a()
print aa.b

the it print : 它打印:

<bound method classobj.b of <class __main__.a at 0x00B2DC00>>

A decorator needs to be a callable object (either a function or an object implementing __call__), where the parameter is the function that has been decorated, and the result is a function that will replace the function that has been decorated, so, to use your example of printing 'sss' instead of printing 'aaa': 装饰器需要是一个可调用的对象(一个函数或一个实现__call__的对象),其中参数是已经装饰的函数,结果是一个函数,它将替换已经装饰的函数,因此,使用打印'sss'而不是打印'aaa'的示例:

>>> def a(f):
...     def replacementfunc():
...         print 'sss'
...     return replacementfunc;
... 
>>> @a
... def b():
...     print 'aaa'
... 
>>> b()
sss

Or, a more elaborate example: 或者,更详细的例子:

>>> class print_decorator(object):
...     def __init__(self,text):
...        self.text = text;
...     def __call__(self,f):
...        def replacement():
...            print self.text;
...        return replacement;
... 
>>> @print_decorator("Hello world!")
... def b():
...     print 'aaa';
... 
>>> b()
Hello world!

Edit 编辑
As for your updated question, you need to look at the documentation for @property . 至于您更新的问题,您需要查看@property的文档。 It's not clear exactly what you are trying to accomplish, although my guess is that you want: 目前尚不清楚你想要完成什么,虽然我的猜测是你想要的:

class a:
    @property
    def b(self):
        return 'sss'

aa=a()
print aa.b # prints 'sss', whereas without @property, prints <function ... >

The line @a() means " a is a callable, returning some other callable, which can be called with a function as its only argument and will return a callable". @a()表示“ a是可调用的,返回一些其他可调用的,可以使用函数作为其唯一参数调用,并返回可调用的”。 If you're not familiar with the term callable , it's just a generalization of function : it can be a function, a class, or an instance of a class that has a __call__ method. 如果您不熟悉术语callable ,它只是function的泛化:它可以是函数,类或具有__call__方法的类的实例。

Your def a is returning None , so you're clearly violating the "decorator contract" that you're requesting with the @a() syntax. 你的def a返回None ,所以你明显违反了你用@a()语法请求的“装饰者合同”。

If you just used @a , without the () , then a would have to accept the function as its argument and return a callable. 如果你只是使用@a ,没有()那么a必须接受功能作为它的参数和返回调用。

I'm not sure what you're trying to accomplish, but if it's just "print something in a decorator for a function, then this would work: 我不确定你要完成什么,但如果它只是“在装饰器中为函数打印一些东西,那么这将起作用:

def a():
    print 'sss'
    return lambda f: f

Now you can use @a() and live happily ever after, since this version does respect the "decorator contract" I explained in the first paragraph. 现在你可以使用@a()并从此过上幸福的生活,因为这个版本确实尊重我在第一段中解释的“装饰者合同”。

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

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