开发者

Save function in tuple without executing

I have开发者_如何学Go a tuple like the following:

self.tagnames = (('string', self.do_anything()),)

It should execute a specific function if a string matches to another. However, when I initialize self.tagnames, it seems to execute the function already.

How can I fix my issue without executing the function on startup?


self.tagnames = (('string', self.do_anything),)

The () is a function call. If you want to defer the call until later, and just include the function reference without the parens like so.


self.tagnames = (('string', self.do_anything),)

You invoke a function by using parens with an argument list:

len is a function, len(s) is invoking that function on the argument s. Simply using the function's name gets you the function. Leave off the parenthesized argument list, and you are no longer invoking the function.


You should just remove the parenthesis:

self.tagnames = (('string', self.do_anything),)

Clearly self.do_anything() calls the method immediately, instead self.do_anything returns what in Python is called a "bound method", i.e. it's a callable object to which you can pass just the parameters (if any) and that will result in calling the method on the specific instance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜