Python: confused with classes, attributes and methods in OOP

Posted by user1586038 on Stack Overflow See other posts from Stack Overflow or by user1586038
Published on 2012-09-15T09:36:19Z Indexed on 2012/09/15 9:37 UTC
Read the original article Hit count: 203

Filed under:
|

A.

Am learning Python OOP now and confused with somethings in the code below.

Question: 1. def init(self, radius=1): What does the argument/attribute "radius = 1" mean exactly? Why isn't it just called "radius"?

  1. 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) """

B.

Question:

  1. In the code below, why is the attribute/argument "name" missing in the brackets?

  2. 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

"""

© Stack Overflow or respective owner

Related posts about python

Related posts about oop