Python Calling Methods Of Class Using Different Ways
Solution 1:
In your example, you're doing:
obj = RSA
which is just binding the name obj
to whatever was bound to RSA
, which is the class RSA
in your instance. Then you're doing:
obj().fast_powering(…)
Which is equivalent to creating an instance of RSA
and calling the method fast_powering
on it. Note that this way, you'll get a new instance of RSA
on each call, which is probably not what you want. You'll also have noticed, that the __init__
method has been called in the line cited above. Also consider:
>>>classRSA:...def__init__(self):...print("foo")...>>>obj = RSA>>>obj() is obj()
foo
foo
False
Here we see that the statement obj() is obj()
in fact creates two objects, which are of course not identical. This is opposed to your first example, as demonstrated using:
>>>classRSA:...def__init__(self):...print("foo")...>>>obj = RSA()
foo
>>>obj is obj
True
Solution 2:
After obj = RSA
, both RSA
and obj
refer to the same class - you're not creating an instance of RSA
, however. Consider this:
classFoo:
def__init__(self):
print'Foo'
Bar = Foo
Foo()
Bar()
Output:
Foo Foo
In terms of which way is more preferable: it really depends on what you are trying to do. But in general, the first method is preferable, unless you have a good reason to be using the second method.
Solution 3:
Well... It turns out that class RSA
is a instance too so you can store it in variables (which is what your second method is doing), although I need to point out you're not actually doing the same thing in both of them. When you do:
val=obj().fast_powering(10,2,obj().CONST_MOD)
You are actually calling the RSA's constructor twice (you've got two obj()
calls), and thus you'll see two "created" messages on the console.
There is a lot of weird ways of doing the same thing in Python. For readability sake, I prefer the "regular" way (the one shown on your first method),but here's a quick example of things that are legally doable:
#!/usr/bin/env pythonclassRSA(object):
CONST_MOD=2def__init__(self):
print"created"deffast_powering(self,number,power,mod):
print"powering"defmethod1_good():
obj=RSA() # here instant of class is created
val=obj.fast_powering(10,2,obj.CONST_MOD) # and we call method of objectprint val
defmethod2_bad():
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)
print val
defmethod3_badToo():
getattr(RSA(), "fast_powering")(10,2,RSA().CONST_MOD)
defmethod4_areYouNuts():
for key, val inglobals().iteritems():
ifisinstance(val, type) and (key == "RSA"):
obj = val()
getattr(obj, "fast_powering")(10,2,obj.CONST_MOD)
breakif __name__ == "__main__":
print"Works with method1?"
method1_good()
print"Works with method2?"
method2_bad()
print"Works with method3?"
method3_badToo()
print"Works with method4?"
method4_areYouNuts()
Maybe this may give you some things to look for, such as: Globals and Locals Gettattr and setattr (1 and 2)
And if you want to dig a bit more... the crazy things you can do with metaclasses... (explained in one of the best answers I've ever seen in StackOverflow)
Post a Comment for "Python Calling Methods Of Class Using Different Ways"