函数,类,绑定,未绑定? [英] functions, classes, bound, unbound?

查看:75
本文介绍了函数,类,绑定,未绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些产生错误的示例代码:


class Test(object):

def greet():

print" Hello"


t = Test()

t.greet()

TypeError:greet()take没有参数(1个给定)


好​​的。那讲得通。 t.greet()是一个绑定方法,所以

会自动将实例对象中继到greet(),因为greet()

定义为no参数 - >错误。


嗯...我通过测试几个例子知道如果我调用一个函数

' class,我使用

函数调用之前的类名,然后我必须手动发送实例。因此,基于该错误消息的

,我将使用类名

调用该方法,而不是发送实例对象:


Test.greet()


TypeError:非绑定方法greet()必须使用Test实例调用

第一个参数(没有任何改为)

解决方案

2007年3月24日20:24:36 -0700,7stud< bb ******** **@yahoo.comwrote:


以下是一些产生错误的示例代码:



[ snip]


为什么人们绝对喜欢*用* b $ b Python做奇怪和丑陋的事情?除了比赛之外,我没有看到很多人在其他(普通)语言上尝试这种类型的东西。


跟我说: Python功能强大,但是我只会使用那个功能*来代替

漂亮而简单的代码。


进一步阅读:
http://www.python.org/doc/Humor.html# zen


-

Felipe。


2007年3月24日星期六20:24:36 -0700,7stud写道:


以下是一些产生错误的示例代码:


class测试(对象):

def greet():

print" Hello"


t = Test()

t.greet()

TypeError:greet()不带参数(给定1个)


好​​的。那讲得通。 t.greet()是一个绑定方法,所以

会自动将实例对象中继到greet(),因为greet()

定义为no参数 - >错误。


嗯...我通过测试几个例子知道如果我调用一个函数

' class,我使用

函数调用之前的类名,然后我必须手动发送实例。因此,基于该错误消息的

,我将使用类名

调用该方法,而不是发送实例对象:


Test.greet()


TypeError:非绑定方法greet()必须使用Test实例调用

第一个参数(没有任何改为)



某个地方是否隐藏着一个问题,或者您只是报告了您已经学到并且感到兴奋的事实?

实例方法总是需要一个self参数,即使你从类中调用它们。这就是为什么你必须手动提供实例,如果你从课堂上打电话给




如果你想创建一个方法,不要自我辩论,

看一下staticmethod函数。还有一个类方法

的功能。


-

史蒂文。


7stud写道:


以下是一些产生错误的示例代码:


class Test(对象):

def greet():

print" Hello"


t = Test( )

t.greet()

TypeError:greet()不带参数(给定1个)



[snip]


Test.greet()

TypeError:必须使用Test实例调用非绑定方法greet()

第一个参数(没有取而代之)



类中的方法通常需要一个实例作为他们的

第一个参数,但你的greet()方法不带参数。这是

因为类不直接调用函数,它们将它转换为

一个''未绑定方法''对象::


>> class Test(object):



... def greet():

...打印''你好''

...
< blockquote class =post_quotes>


>> Test.greet



< unbound method Test.greet>


>> Test.greet()



Traceback(最近一次调用最后一次):

文件"< interactive input>",第1行,< module>

TypeError:非绑定方法greet()必须使用Test实例调用

作为第一个参数(没有取而代之)


如果你真的想要获得原来的功能,还有一对

的选择。您可以浏览班级的__dict__,或者您可以使用@staticmethod装饰器包装

您的方法(告诉班级不要使用
包裹该功能)尝试使用它)::


>> Test .__ dict __ [''greet '']



< function greet at 0x00E708B0>


>>测试.__ dict __ [''greet'']()



Hello


>>类测试(对象):



... @staticmethod

... def greet():

...打印''你好''

...


>> Test.greet



<函数greet at 0x00E7D2F0>


>> Test.greet()



你好


STeVe


Here is some example code that produces an error:

class Test(object):
def greet():
print "Hello"

t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)

Ok. That makes sense. t.greet() is a "bound method", so something
automatically relays the instance object to greet(), and since greet()
is defined with no parameters-->Error.

Hmmm...I know from testing several examples that if I call a function
that''s defined inside a class, and I use the class name preceding the
function call, then I have to send the instance manually. So, based
on that error message, I''ll just call the method using the class name
and not send the instance object:

Test.greet()

TypeError: unbound method greet() must be called with Test instance as
first argument (got nothing instead)

解决方案

On 24 Mar 2007 20:24:36 -0700, 7stud <bb**********@yahoo.comwrote:

Here is some example code that produces an error:

[snip]

Why do people absolutely *love* to do weird and ugly things with
Python? Contests apart, I don''t see lots of people trying this kind of
things on other (common) languages.

Say with me: "Python is powerful, but I''ll use that power *only* for
beautiful and straightforward code."

Further reading:
http://www.python.org/doc/Humor.html#zen

--
Felipe.


On Sat, 24 Mar 2007 20:24:36 -0700, 7stud wrote:

Here is some example code that produces an error:

class Test(object):
def greet():
print "Hello"

t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)

Ok. That makes sense. t.greet() is a "bound method", so something
automatically relays the instance object to greet(), and since greet()
is defined with no parameters-->Error.

Hmmm...I know from testing several examples that if I call a function
that''s defined inside a class, and I use the class name preceding the
function call, then I have to send the instance manually. So, based
on that error message, I''ll just call the method using the class name
and not send the instance object:

Test.greet()

TypeError: unbound method greet() must be called with Test instance as
first argument (got nothing instead)


Is there a question hidden there somewhere, or are you just reporting on a
fact you have learned and are excited about?
Instance methods always need a self parameter, even if you call them from
the class. That''s why you have to provide the instance by hand if you call
them from the class.

If you are trying to create a method that doesn''t take a self argument,
have a look at the staticmethod function. There''s also a classmethod
function as well.

--
Steven.


7stud wrote:

Here is some example code that produces an error:

class Test(object):
def greet():
print "Hello"

t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)

[snip]

Test.greet()

TypeError: unbound method greet() must be called with Test instance as
first argument (got nothing instead)

Methods in a class are generally expected to take an instance as their
first argument, but your greet() method takes no arguments. This is
because classes don''t invoke the function directly, they convert it to
an ''unbound method'' object::

>>class Test(object):

... def greet():
... print ''Hello''
...

>>Test.greet

<unbound method Test.greet>

>>Test.greet()

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: unbound method greet() must be called with Test instance
as first argument (got nothing instead)

If you really want to get to the original function, there are a couple
of options. You can go through the class''s __dict__, or you can wrap
your method with a @staticmethod decorator (which tells the class not to
wrap the function when you try to use it)::

>>Test.__dict__[''greet'']

<function greet at 0x00E708B0>

>>Test.__dict__[''greet'']()

Hello

>>class Test(object):

... @staticmethod
... def greet():
... print ''Hello''
...

>>Test.greet

<function greet at 0x00E7D2F0>

>>Test.greet()

Hello

STeVe


这篇关于函数,类,绑定,未绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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