初始化一个功能表 [英] Init a table of functions

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

问题描述

如何对下面的程序进行编程?

PYTHON声称在初始化K时它并不知道''foo'!

唯一的方法我得到这个工作是在

__init__ func中执行K的初始化。但每次对象都会运行!

当使用没有''foo''的函数名称时,我不知道
知道如何调用它!


感谢您的帮助。


#! / bin / env python

# - * - 编码:iso-8859-15 - * -


class foo:

def f1():

print" f1"

f1 = staticmethod(f1)


def f2():

print" f2"

f2 = staticmethod(f2)


K = {" f1" :foo.f1,\

" f1" :foo.f2 \

}

def main():

foo.K [" f1"]()

foo.K [" f2"]()


if __name__ ==" __ main __":

main()

解决方案

Paulo da Silva写道:

如何编写类似下面的程序?
PYTHON声称在初始化K时它并不知道''foo'!我唯一能让它工作的方法就是在__init__函数中执行K的初始化。但每次对象都会运行!
当使用没有''foo''的函数名称时,我不知道如何调用它!

谢谢任何帮助。

#! / bin / env python
# - * - 编码:iso-8859-15 - * -
类foo:
def f1():
print" f1"
f1 = staticmethod(f1)


打印" f2"
f2 = staticmethod(f2)

" f1" :foo.f2 \
}

def main():
foo.K [" f1"]()
foo.K [" f2" ;]()

如果__name__ ==" __ main __":
main()




self.f1和self。 f2应该适用于任何类。试试吧。


好吧,我试过自己,它和foo说的一样。所以我只是在没有self或foo的情况下调用

f1()和f2(),因为它们应该在

范围内并且它在这个范围内工作但是进一步抱怨,请参阅下面:


文件

" C:\Python23 \Lib \site-packages \Pythonwin \ pywin \ framework \scriptutils的.py" ,
第310行,在RunScript中

exec codeObject在__main __.__ dict__

文件" .. \ My Documents \CODE \ test_dumpscript.py",第20行,在?

main()

文件" .. \ My Documents \CODE \\\ test_dumpscript.py",第16行,in main

foo.K [" f1"]()

TypeError:''staticmethod''对象不可调用

< blockquote class =post_quotes>



我如何编写类似下面的程序?
PYTHON声称它没有'在初始化K时我不知道''foo'!
我让这个工作的唯一方法就是在__init__ func中执行K的初始化。但每次对象都会运行!
当使用没有''foo''的函数名称时,我不知道如何调用它!

谢谢任何帮助。

#! / bin / env python
# - * - 编码:iso-8859-15 - * -
类foo:
def f1():
print" f1"
f1 = staticmethod(f1)


打印" f2"
f2 = staticmethod(f2)

" f1" :foo.f2 \
}

def main():
foo.K [" f1"]()
foo.K [" f2" ;]()

如果__name__ ==" __ main __":
main()



Paulo da Silva写道:

我如何编程类似下面的程序?
PYTHON声称在初始化K时它不知道''foo'!
唯一的方法我得到这个工作是在__init__功能中执行K的初始化。但每次对象都会运行!
当使用没有''foo''的函数名称时,我不知道如何调用它!

谢谢任何帮助。




将表移到类定义之外


class foo(object):

def f1():

打印''f1''

f1 = staticmethod(f1)


foo .K = {''f1'':foo.f1,

}


foo.K [''f1'']()


-

Robert Kern
rk***@ucsd.edu


在地狱的地方,草地长得很高

梦想的坟墓是否能够死亡。

- Richard Harter


How do I program something like the folowing program?
PYTHON claims it doesn''t know about ''foo'' when initializing K!
The only way I got this to work was doing the init of K in
the __init__ func. But this runs everytime for each object!
When using the name of the function without ''foo'', I don''t
know how to call it!

Thanks for any help.

#! /bin/env python
# -*- coding: iso-8859-15 -*-

class foo:
def f1():
print "f1"
f1=staticmethod(f1)

def f2():
print "f2"
f2=staticmethod(f2)

K={"f1" : foo.f1, \
"f1" : foo.f2 \
}
def main():
foo.K["f1"]()
foo.K["f2"]()

if __name__ == "__main__":
main()

解决方案

Paulo da Silva wrote:

How do I program something like the folowing program?
PYTHON claims it doesn''t know about ''foo'' when initializing K!
The only way I got this to work was doing the init of K in
the __init__ func. But this runs everytime for each object!
When using the name of the function without ''foo'', I don''t
know how to call it!

Thanks for any help.

#! /bin/env python
# -*- coding: iso-8859-15 -*-

class foo:
def f1():
print "f1"
f1=staticmethod(f1)

def f2():
print "f2"
f2=staticmethod(f2)

K={"f1" : foo.f1, \
"f1" : foo.f2 \
}
def main():
foo.K["f1"]()
foo.K["f2"]()

if __name__ == "__main__":
main()



self.f1 and self.f2 should work within any class. Try that.


Well, I tried self and it said the same as with foo. So I just called
f1() and f2() without self or foo in front since they should be within
scope and it worked to that extent but complained further, see below:

File
"C:\Python23\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py" ,
line 310, in RunScript
exec codeObject in __main__.__dict__
File "..\My Documents\CODE\test_dumpscript.py", line 20, in ?
main()
File "..\My Documents\CODE\test_dumpscript.py", line 16, in main
foo.K["f1"]()
TypeError: ''staticmethod'' object is not callable


How do I program something like the folowing program?
PYTHON claims it doesn''t know about ''foo'' when initializing K!
The only way I got this to work was doing the init of K in
the __init__ func. But this runs everytime for each object!
When using the name of the function without ''foo'', I don''t
know how to call it!

Thanks for any help.

#! /bin/env python
# -*- coding: iso-8859-15 -*-

class foo:
def f1():
print "f1"
f1=staticmethod(f1)

def f2():
print "f2"
f2=staticmethod(f2)

K={"f1" : foo.f1, \
"f1" : foo.f2 \
}
def main():
foo.K["f1"]()
foo.K["f2"]()

if __name__ == "__main__":
main()



Paulo da Silva wrote:

How do I program something like the folowing program?
PYTHON claims it doesn''t know about ''foo'' when initializing K!
The only way I got this to work was doing the init of K in
the __init__ func. But this runs everytime for each object!
When using the name of the function without ''foo'', I don''t
know how to call it!

Thanks for any help.



Move the table outside of the class definition

class foo(object):
def f1():
print ''f1''
f1 = staticmethod(f1)

foo.K = {''f1'': foo.f1,
}

foo.K[''f1'']()

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


这篇关于初始化一个功能表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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