Reputation: 79
I'm learning Python OOP now and confused with somethings in the code below.
Questions:
def __init__(self, radius=1):
What does the argument/attribute "radius = 1" mean exactly?
Why isn't it just called "radius"?
The method area() has no argument/attribute "radius". Where does it get its "radius" from in the code? How does it know that the radius is 5?
class Circle:
pi = 3.141592
def __init__(self, radius=1):
self.radius = radius
def area(self):
return self.radius * self.radius * Circle.pi
def setRadius(self, radius):
self.radius = radius
def getRadius(self):
return self.radius
c = Circle()
c.setRadius(5)
Also,
In the code below, why is the attribute/argument name
missing in the brackets?
Why was is not written like this: def __init__(self, name)
and def getName(self, name)
?
class Methods:
def __init__(self):
self.name = 'Methods'
def getName(self):
return self.name
Upvotes: 2
Views: 2467
Reputation: 1125148
The def method(self, argument=value):
syntax defines a keyword argument, with a default value. Using that argument is now optional, if you do not specify it, the default value is used instead. In your example, that means radius
is set to 1
.
Instances are referred to, within a method, with the self
parameter. The name
and radius
values are stored on self
as attributes (self.name = 'Methods'
and self.radius = radius
) and can later be retrieved by referring to that named attribute (return self.name
, return self.radius * self.radius * Circle.pi
).
I can heartily recommend you follow the Python tutorial, it'll explain all this and more.
Upvotes: 8
Reputation: 376
def __init__(self, radius=1):
self.radius = radius
This is default value setting to initialize a variable for the class scope.This is to avoid any garbage output in case some user calls c.Area()
right after c = Circle()
.
In the code below, why is the attribute/argument "name" missing in the brackets?
In the line self.name = 'Methods'
you are creating a variable name
initialized to string value Methods
.
Why was is not written like this: def init(self, name) and def getName(self, name)?
self.name
is defined for the class scope. You can get and set its value anywhere inside the class.
Upvotes: 1
Reputation: 102039
The syntax radius = 1
specifies a parameter "radius" which has a default value of 1
:
def my_func(param=1): ... print(param) ... my_func() #uses the default value 1 my_func(2) #uses the value passed 2
Note that in python there exists more kinds of parameters: positional and keyword parameters, or both.
Usually parameters can be assigned both using the positional notation and the keyword:
>>> def my_func(a,b,c):
... print (a,b,c)
...
>>> my_func(1,2,3)
(1, 2, 3)
>>> my_func(1,2,c=3)
(1, 2, 3)
self
parameter is used to pass the instance on which the methods are called. You can think of self
as being the this
of Java. But you must always use it to access instance attributes/methods. You can't call just area()
, you must say self.area()
.When you do self.attribute = 1
you create a new attribute attribute
with value 1
and assign it to the instance self
. So in the area()
method self.radius
refers to the radius
attribute of the self
instance.
The __init__
method is a special method. It's something similar to a constructor.
It is called when you instantiate the class. Python has a lot of these "special methods", for example the method __add__(self, other)
is called when using the operator +
.
Upvotes: 0