In Java, objects are instantiated with a complete copy of the functions from the class linked to them. This is one of the reasons that the Spring Framework has been so successful. Spring helps you cut down on the memory that the Java VM uses if you create many temporary data objects, and other service objects that are served up by Spring as singletons that effectively carry all the functions.
I was just wondering if this true in Python? It seems like it isn’t. But that means that if you mess with dict for an object, you are changing all that function for all copies of that class, right?
For example:
class MyObj:
a = 23
def __init__(self, b):
self.b = b
def __add__(self, c):
return self.b + c
If I create an array of MyObj, is there one instantiation of __add__ for each, or just one for all of them?
There is just one instance of the function, which is stored inside the class. The function gets called with a reference to a class instance as the first argument, which is traditionally called
self.So, here are three equivalent ways to call the
.__add__()method function:Also, in Python, the “type signature” of a function is not part of the function’s name. The only
.__add__()method you get is the one you declared. In Python you would simply write your code to make the one function do all the different jobs you might want it to do. For example, if you wanted.__add__()to convert a string into an integer to makex + "3"return 5, you would do it like so:Note 0: It is best to declare your class as inheriting from
object. In Python 3.x, your class will inherit fromobjectwhether you declare it this way or not, but in Python 2.x you will get an “old-style class” unless you declare it with(object)after the class name.Note 1: we don’t bother to check the type of the
otherargument; we just try to coerce it to anintvalue. This is “Duck Typing” in action. Now not only will a string be coerced to an integer, but anything that can successfully be coerced to anintwill work.Sometimes, to make one function do multiple jobs, you may need extra arguments. You can make them optional so that you don’t need to specify them every time you call the function. You can read more about this in a Python tutorial; here’s one place: http://diveintopython.net/power_of_introspection/optional_arguments.html
Finally, in Python, you need to explicitly use
MyObj.ato refer to that class variablea. If you just useain a member function, you will get the usual name resolution rules, which will look in the global namespace; the object namespace isn’t special.You could also get to a class variable like so:
But this will only work if it is a new-style class that inherits from
object!