我有一些類:蟒蛇採用不同的方式調用類的方法
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++的方式,但無論如何 我非常驚訝地它也能工作!
這裏究竟發生了什麼?哪種方式更受歡迎?這對我來說有些神祕。
在此先感謝您的回覆!
+1對於省略號使用正確的字形。開玩笑,答案很好,但我希望看到更多。 – delnan