在Python中从self内部引用self
我什至不确定我的问题标题是否正确,但无论如何我都会开火。但如果事实证明标题不完全正确,我想道歉。另外,我必须承认,就Python而言,我不是专家,所以如果我的问题很愚蠢,那就笑一笑吧!
我定义了一个使用 code.interpreter 模块的 python 类。 (基本上,我正在尝试编写一个原始控制台。)我可以将字符串传递给解释器,并且一切正常。但是,我想在此类之外进行字符串解析,因此我尝试做的是将从命令行读取的字符串传递给我的解析器函数,该函数又返回一个字符串。 (它将原始字符串扩展为有效的 python 语句。)我获取该字符串,并将其传递给解释器。这仍然很好用。但是,当返回的字符串包含对我的原始类中定义的函数的引用时,它会中断,并且 python 会抱怨 self.whatever 未定义。也许,下面的代码片段会让事情变得更清晰一些
class myclass():
...
parsed_line = parser.parse_line('line to parse')
code.InteractiveInterpreter.runsource( parsed_line )
def self.do_something( self ):
print 'I have done something'
pass
,而我的外部函数
def parse_line( line ):
if 'line' = 'line to parse'
return 'self.do_something()'
嗯,它会崩溃。如果我修改我的解析器,让它
def parse_line( line ):
return 'print 12'
工作正常,并愉快地打印 12。实际上,对 self.do_something 的引用并不重要。即使我尝试对 self.a 进行简单的赋值,它仍然会中断。
我的问题是,如何克服上述问题?我确实必须引用 self.whatever,因为函数 do_something 对 myclass 中的类成员之一进行操作。
谢谢,
佐尔坦
I am not even sure whether the title of my question is correct, but I will fire away anyway. But I would like to apologise, if it turns out that the title is not entirely correct. Also, I have to admit that I am not a guru, as far as python is concerned, so if my question is stupid, just have a good laugh!
I have defined a python class that uses the code.interpreter module. (Basically, I am trying to write a primitive console.) I can pass strings to the interpreter, and everything works fine. However, I would like to do the string parsing outside of this class, so what I have tried to do is to pass the string that I read from the command line to my parser function, which in turn, returns a string. (It expands the original string into a valid python statement.) I take this string, and pass it to the interpreter. This still works fine. However, when the returned string contains a reference to a function defined in my original class, it breaks, and python complains that self.whatever is not defined. Perhaps, the following snippet would make things a bit clearer
class myclass():
...
parsed_line = parser.parse_line('line to parse')
code.InteractiveInterpreter.runsource( parsed_line )
def self.do_something( self ):
print 'I have done something'
pass
and my external function
def parse_line( line ):
if 'line' = 'line to parse'
return 'self.do_something()'
Well, it will break. If I modify my parser as
def parse_line( line ):
return 'print 12'
it works all right, and happily prints 12. Actually, the reference to self.do_something is not really important. Even if I tried to do a simply assignment to, say, self.a, it would still break.
My question is, how can one overcome the problem described above? I really have to refer to self.whatever, because the function do_something operates on one of the class members in myclass.
Thanks,
Zoltán
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将这部分:更改
为:
当您调用方法时,您使用 self.some_method()。当你定义它时,你只需使用参数
self
来定义它,该参数将被隐式传入。编辑:
你还需要给你的交互式解释器一些帮助来告诉你它应该在什么上下文中运行,通过传入 locals():
将 this: 更改
为如下所示:
Change this part:
to just:
When you call a method, you use self.some_method(). When you define it, you just define it with the parameter
self
, which will be implicitly passed in.EDIT:
You also need to give your Interactive Interpreter a little help to tell it what context it should run in, by passing in the locals():
change this:
to something like this:
self
在 Python 中并不像其他语言那样具有魔力;它只是按照惯例用作实例方法调用的第一个参数,因为实例作为此参数传递给方法。它在类声明中没有任何意义,仅在实例方法体内有意义。self
is not magic in Python like in some other languages; it's merely conventionally used as the first parameter for instance method calls, since the instance is passed to the method as this parameter. It has no meaning within a class declaration, only within the body of the instance method.如果你的类 C 是固定的,你可以调用该方法,
但这不是一个好的风格。调用外部函数的原因是什么???
你可以定义
你能详细说明你想做什么吗?我认为还有更好的、更多的
面向对象的方式来做到这一点。
If your class C is fixed, you can call the method as
But this is not good style. What is the reason to call a function outside ???
You could define
Could you elaborate what you want to do ? I think there is a better, more
object oriented way to do it.