开发者

referring to self from within self in python

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.whate开发者_JAVA技巧ver 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


Change this part:

def self.do_something( self ):
    print 'I have done something'

to just:

def do_something( self ):
    print 'I have done something'

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:

code.InteractiveInterpreter.runsource( parsed_line )

to something like this:

interpreter = code.InteractiveInterpreter(locals())
interpreter.runsource( parsed_line )


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.


If your class C is fixed, you can call the method as

Myclass.do_something(self)

But this is not good style. What is the reason to call a function outside ???

You could define

def parse_something(obj, line):
    obj.do_something()

Could you elaborate what you want to do ? I think there is a better, more object oriented way to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜