动态类方法的误解 [英] Dynamic class methods misunderstanding

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

问题描述

大家好,


我对动态类方法有误解。我不期望

这种行为:


在[2]中:课堂测试:

......:def __init __(self,method):

...:self.method = method

...:self.method()

。 ..:


在[3]中:def m(self):print self

...:


在[4]中:test(m)

------------------------------- --------------------------------------------

exceptions.TypeError Traceback(最近一次电话

last)


/ cygdrive / c / Documents and Settings / Wmill /< console>


/ cygdrive / c / Documents and Settings / Wmill /< console> in __init __(self,method)


TypeError:m()只取1个参数(0给定)

---------- -------------------------------------------------- -----------------


为什么在self.method()中没有得到隐含的self参数

电话?我怎样才能使它成为该类的正确成员,以便

self.method()调用可以使用上面的m。功能?


和平

Bill Mill

bill.mill at gmail.com

解决方案

Le Fri,2005年1月28日10:20:30 -0500,Bill Millaécrit:

大家好,

我对动态类方法有误解。我不期望这种行为:

在[2]中:班级考试:
...:def __init __(自我,方法):
。 ..:self.method =方法
...:self.method()
...:

在[3]中:def m(self):print self
......:

在[4]中:test(m)
--------------------- -------------------------------------------------- ----
exceptions.TypeError Traceback(最近一次致电
最后)

/ cygdrive / c / Documents and Settings / Wmill /< console>

/ cygdrive / c / Documents and Settings / Wmill /< console> in __init __(self,method)

TypeError:m()只取1个参数(0给定)
------------------ -------------------------------------------------- ---------

为什么在self.method()
调用中没有得到隐含的self参数?我如何使它成为该类的适当成员,以便
self.method()调用可以与上述m一起使用。功能?
def m(self):没有正确缩进。所以这里,m是一个模块级别

函数,而不是你班级的方法。
和平
Bill Mill
bill.mill at gmail.com


>

为什么在self.method()
调用中没有得到隐含的self参数?我如何使它成为该类的适当成员,以便
self.method()调用可以与上述m一起使用。功能?




使用new.instancemethod:


导入新


类测试:

def __init __(自我,方法):

self.m = new.instancemethod(方法,自我,测试)


def m(个体经营):

打印自我

测试(米).m()

-

Diez B. Roggisch


我看到你在尝试做什么。但是,你的代码,如果它运行DID,

会导致一个方法被添加到对象,而不是对象'

类!修改类本身,而不是对象,如下所示:


| class测试:

| def __init __(self):

| self.method()

|

| def m(self):

|打印自我

|

| setattr(测试,''方法'',m)

|测试()


Hello all,

I have a misunderstanding about dynamic class methods. I don''t expect
this behavior:

In [2]: class test:
...: def __init__(self, method):
...: self.method = method
...: self.method()
...:

In [3]: def m(self): print self
...:

In [4]: test(m)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most recent call
last)

/cygdrive/c/Documents and Settings/Wmill/<console>

/cygdrive/c/Documents and Settings/Wmill/<console> in __init__(self, method)

TypeError: m() takes exactly 1 argument (0 given)
-----------------------------------------------------------------------------

Why doesn''t m get the implicit self parameter in the self.method()
call? How would I make it a proper member of the class, so that a
self.method() call would work with the above "m" function?

Peace
Bill Mill
bill.mill at gmail.com

解决方案

Le Fri, 28 Jan 2005 10:20:30 -0500, Bill Mill a écrit :

Hello all,

I have a misunderstanding about dynamic class methods. I don''t expect
this behavior:

In [2]: class test:
...: def __init__(self, method):
...: self.method = method
...: self.method()
...:

In [3]: def m(self): print self
...:

In [4]: test(m)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most recent call
last)

/cygdrive/c/Documents and Settings/Wmill/<console>

/cygdrive/c/Documents and Settings/Wmill/<console> in __init__(self, method)

TypeError: m() takes exactly 1 argument (0 given)
-----------------------------------------------------------------------------

Why doesn''t m get the implicit self parameter in the self.method()
call? How would I make it a proper member of the class, so that a
self.method() call would work with the above "m" function? The "def m(self):" was not properly indented. So here, "m" is a module level
function, not a method of your class.
Peace
Bill Mill
bill.mill at gmail.com



>

Why doesn''t m get the implicit self parameter in the self.method()
call? How would I make it a proper member of the class, so that a
self.method() call would work with the above "m" function?



Use new.instancemethod:

import new

class Test:
def __init__(self, method):
self.m = new.instancemethod(method, self, Test)

def m(self):
print self

Test(m).m()
--
Regards,

Diez B. Roggisch


I see what you''re attempting to do. However, your code, if it DID run,
would result in a method being added to the object, not the object''s
class! Modify the class itself, not the object, as follows:

|class Test:
| def __init__(self):
| self.method()
|
|def m(self):
| print self
|
|setattr(Test, ''method'', m)
|Test()


这篇关于动态类方法的误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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