开始实行这样的分类:
class FooClass(object):
@staticmethod
def static_method(x):
print x
通常,我会把这一类静态方法称作:
FooClass.static_method( bar )
采用这种静态方法,只字体名称和方法名称吗?
class_name = FooClass
method_name = static_method
开始实行这样的分类:
class FooClass(object):
@staticmethod
def static_method(x):
print x
通常,我会把这一类静态方法称作:
FooClass.static_method( bar )
采用这种静态方法,只字体名称和方法名称吗?
class_name = FooClass
method_name = static_method
正如其他答复所建议的那样,你应该向当地人提供我。 如果你称职,需要解决,则使用某种登记处。 起诉人将罚款。 E.g.
class FooClass(object):
@staticmethod
def static_method(x):
print x
registry = { FooClass :FooClass}
(我假定你想在本登记册中增加许多班级)
然后,看管变得几乎微不足道:
getattr(registry[ FooClass ], static_method )("bar")
此处为:
>>> class FooClass(object):
@staticmethod
def static_method(x):
print x
>>> class_name = FooClass
>>> method_name = static_method
>>> getattr(locals().get(class_name), method_name)("bar")
bar
细目:
locals().get(class_name)
首先,确定这一类别。 在这种情形下,使用 locals(
,因为我知道该类别可在当地字典上查阅。 如果当地独裁者不在场,这将失败。
其次,找到这一类方法。
getattr(locals().get(class_name), method_name)
http://docs.python.org/library/Functions.html#getattr”rel=“nofollow”>getattr(
。
最后,采用这种方法。
getattr(locals().get(class_name), method_name)("bar")
页: 1 第一次在包含该类内容的模块上,第二次在班级上。
class_name = Foo
method_name = Bar
cls = getattr(mod, clsname)
method = getattr(cls, method_name)
method(args)
这并不像建造一个登记册那样灵活(你可以做一个校正员),但如果你不这样做,那么,这比“far更好的替代方法,比“
注:模块可import
其本身没有不利影响。 因此,这些班级不必在不同的模块中工作。
如果从目前的模块中可以进入这一类别,则您可使用<代码>globals(dict)和getattr
的方法:
class FooClass(object):
@staticmethod
def static_method(x):
print x
class_name = FooClass
method_name = static_method
getattr(globals()[class_name], method_name)("test")
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...