I am new to Python (coming from MATLAB) and was beginning to learn that classes were a good way to modularize a program. I understand that they are like a "blueprint" that can streamline processes, but I think my understanding of that is faulty. I was reading the documentation and came across this example:
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
with an output of
(3.0, -4.5)
And it stumped me. There are no functions known as realpart or imagpart to be passed and yet they are renamed as r and i and instantly work. They don't appear to be built in functions either. How are realpart and imagpart doing anything?
NOTE: I apologize for the ambiguous title and tags, I'm not really sure what this would be called. I'd appreciate suggestions.
2 Answers 2
How are realpart and imagpart doing anything?
They're not doing anything: they're just the names given to the arguments to __init__. We could call them apple and pear if we liked. When you type
x = Complex(3.0, -4.5)
__init__ is passed three arguments:
def __init__(self, realpart, imagpart):
the instance itself (traditionally called self), the first explicit argument passed (3.0), and the second explicit argument passed (-4.5). It then stores these as instance variables in self.r and self.i.
4 Comments
realpart and imagpart a value? That can then be called by their shorter names r and i?__init__. The reason you can refer to them by r and i is because the code assigns those shorter names to them. They don't magically get extra names.realpart and imagpart are given values, yeah, but only for the lifetime of the __init__ method. It's like def f(a,b): return a+b; f(1,2) does bind a=1 and b=2, but only locally. As BrenBarn says, it's the self.r = realpart which pushes the local realpart into the instance.realpart and imagpart are the arguments to the class initializer __init__. When you call Complex(3.0, -4.5), those arguments are passed to that function. The function happens to set some attributes on the object to save the values of those arguments. It does this with the lines
self.r = realpart
self.i = imagpart
These lines create attributes on the object. When you create an object called x, it will have those attributes accessible as x.r and x.i.
I'm not sure why you're asking about "functions" called realpart and imagpart. They are just ordinary variables.
You should read the Python tutorial to familiarize yourself with the basics of Python.
__init__function "inside classes" is markedly different than MATLAB's functions because__init__it creates an instance of a class, in this case Complex. In MATLAB, you define functions that you pass values to and return some kind of result usually. Python can do that too, but what you're doing here is instantiating an object, so you're storingrealpartandimagpartin the object. Unlike python, MATLAB is not object-oriented, though it comes somewhat close with something called functional objects.