【发布时间】:2012-11-20 17:09:36
【问题描述】:
我有一些课:
class RSA:
CONST_MOD=2
def __init__(self):
print "created"
def fast_powering(self,number,power,mod):
print "powering"
我想实例化它并调用fast_powering方法:
def main():
obj=RSA() # here instant of class is created
val=obj.fast_powering(10,2,obj.CONST_MOD) # and we call method of object
print val
而且效果很好!
但我发现我也可以用一些不同的方式来做,比如:
def main():
obj=RSA #do we create a link to the class without creating of object , or what?
val=obj().fast_powering(10,2,obj().CONST_MOD) # and here we do something like
# calling of static method of class in C++ without class instantiation,
# or ?
print val
对不起,我觉得有点 C++ 的方式,但无论如何
令我惊讶的是,它也有效!
这里到底发生了什么?哪种方式更受欢迎?这对我来说有点神秘。
提前感谢您的任何回复!
【问题讨论】: