导入方法 [英] importing a method

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

问题描述




我有一个用大量硬编码方法定义的对象。


Class soandso:

def __init __(自我):

self.this = 0

self.that = 1

def meth1(self):

...

def meth2(个体经营):

...

def custom(self):

传递


我想让用户编写一个声明一个

函数的python模块,以便myprogram可以导入它和属性它是soandso对象的自定义

方法。到目前为止一切都那么好,用Python做一个简单的事情就可以了。


import usermodule

a = soandso()

a.custom = usermodule.function


但是,如果方法必须访问自我属性怎么办(self.this

对于soandso对象的自我和那个?


能做到吗?如果是这样,最恐怖的做法是什么?


提前感谢,

$ b $bFlávio

推荐答案

Flavio< fc ****** @ gmail.com>写道:
Flavio <fc******@gmail.com> wrote:
类soandso:
def __init __(self):
self.this = 0
self.that = 1
def meth1(self) :
...
def meth2(个体经营):
...
def custom(个体经营):
传递

我想要允许用户编写一个声明
函数的python模块,以便myprogram可以导入它并将其归因于soandso对象的自定义
方法。到目前为止一切都很好,用Python做一件简单的事情。

导入usermodule
a = soandso()
a.custom = usermodule.function


显然你还没有测试过这是否有效。它没有:
Class soandso:
def __init__(self):
self.this = 0
self.that = 1
def meth1(self):
...
def meth2(self):
...
def custom(self):
pass

I want to allow the user to write a python module that declares a
function so that myprogram can import it and attribute it to the custom
method of the soandso object. So far so good, that is an easy thing to
do in Python.

import usermodule
a=soandso()
a.custom = usermodule.function
Apparently you haven''t tested whether this works. It doesn''t:
class Foo(object):
... pass

... def bar(* args):
... print" bar()得到参数:",args

... bar(" ;垃圾邮件"," eggs")
bar()得到的参数:(''spam'',''eggs'')Foo.bar_method = bar
Foo.bar_method(" spam", 鸡蛋)
回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:必须使用Foo实例作为第一个参数调用未绑定方法bar()(改为使用str实例)

但是,如果该方法必须访问自身属性(self.this
和self),该怎么办? .that)soandso对象?
class Foo(object): ... pass
... def bar(*args): ... print "bar() got arguments:", args
... bar("spam", "eggs") bar() got arguments: (''spam'', ''eggs'') Foo.bar_method = bar
Foo.bar_method("spam", "eggs") Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method bar() must be called with Foo instance as first argument (got str instance instead)
But, what if the method had to access the self attributes (self.this
and self.that) of the soandso object?




要成为一个方法,该函数必须绑定到一个实例,并且

方法将当

被称为方法时,接收实例作为第一个参数。

要在已定义的函数上执行此操作,请使用new.instancemethod。

import new
help(new.instancemethod)



To become a method, the function must be bound to an instance, and the
method will then receive the instance as the first argument when
called as a method.

To do this on an already-defined function, use new.instancemethod.
import new
help(new.instancemethod)




-

\有一次警察拉我过来停车牌。他告诉他们,''你没看到停车标志吗?''我说,'是的,但我|

_o__) 我相信我读到的一切。 - Steven Wright |
Ben Finney



--
\ "One time a cop pulled me over for running a stop sign. He |
`\ said, ''Didn''t you see the stop sign?'' I said, ''Yeah, but I |
_o__) don''t believe everything I read.''" -- Steven Wright |
Ben Finney


为什么不让你的用户子类soandso并覆盖来自soandso import的自定义?




定义soandso

class MyClass(s​​oandso):

def custom(self):

self.theother = 3


c = MyClass()

why not just have your user subclass "soandso" and override the
definition of "custom"?

from soandso import soandso
class MyClass(soandso):
def custom(self):
self.theother = 3

c = MyClass()


因为,当导入用户函数并将其归结为

自定义方法时,soandso已经被实例化并包含

用户需要访问的信息。

Because, by the time the user function is imported and attributed to
the custom method, soandso has already been instantiated and contains
the information tha needs to accessed by the user''s function.


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

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