Objects and Classes


Hi, is this the answer to 7.10A) and B)?

7.10 A) There is no init method.
7.10 B) self.radius is not created in the init method. Also, radius is supposed to be in the parentheses init(self) in order for you to create self.radius. Also, the method setRadius does not contain self between the parentheses. So 7.10B) should be like:

class A:
    # Construct an object of the class
    def __init__(self, radius = 3)
        self.radius = radius

    def setRadius(self, radius)
    self.radius = radius


46 AM


who says classes need an __init__() method? Its not mandatory to have an init method (constructor), although the comment indeed suggest that def A is going to be used as constructor, which is wrong. So i think you are partial right.

i furthermore have problems with that the naming convention is not used for methods in both example a and b:

methods should be all lowercase. But this are not rules. More convention/recommendation, so it should be set_radius, i will write for example b in a different reply


This is what the author wrote in the solutions.

14 AM


the instance variable (self.radius) should be defined in the __init__(), good spot. If you want to use a named argument:

def __init__(self, radius = 3):

or not:

def __init__(self, radius):

depends on your program implementation, but realize both is possible. You can always give 3 as argument when creating an instance

this makes def set_radius redundant, unless you plan on using it as setter method, but that doesn’t seem to be the case.

based on the solutions, yea, the comment was crucial, incorrect use of the constructor. But classes do not need to have constructor/init method

i don’t agree with the solution of b, which suggest:

class A:
    # Construct an object of the class
    def __init__(self):
        self.radius = 3

But there are multiply solutions possible:

class A:
    # class variable
    self.radius = 3

class A:
    # Construct an object of the class
    def __init__(self, radius):
        self.radius = radius

instance = A(3)

or the solution you applied. Wrong and right is so subjective in programming, there are multiple ways to solve a problem, you need the best solution.


Thanks! One more question.
![50 AM|652x500]
56 AM

In this program,

on line 10, circle2.radius will be 25 because the constructor Circle(25) passes in radius as 25 and ignores the default radius argument in the initializer method in the class Circle.
On line 20, circle2.radius is reassigned to 100. So, does this change the value of radius in the initializer method in Circle class? So radius = 1 and then = 25 and now = 100?


The initialization method runs on object creation, it’s has exited by the time you assign circle2 to that object. You’re changing the object’s radius attribute, same as what the initialization method did

self.radius = ...
circle2.radius = ...

(self in __init__ refers to the same object as circle2 in main)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.