When should I create a new method vs placing it in init?

When do you create a new method and when do you put it in the __init__? I understand that if it’s a long method it’s better but when you can return it in a single function is there any reason why we would make more methods? I think this looks a lot cleaner:


 class Diameter():
    pi = 3.14159
    def __init__(self, diameter):
        self.radius = diameter/2
        self.area = Diameter.pi * self.radius ** 2
        self.circumference = Diameter.pi * self.radius * 2

        
pizza = Diameter(12)

print(f"Pizza radius: {pizza.radius}")
print(f"Pizza area: {pizza.area}")
print(f"Pizza circumference: {pizza.circumference}")

__init__() is called when you instantiate a class:

Diameter() # create class instance which will trigger the init method

separation of concerns, easier to test.

also, what if you change the diameter:

class Diameter():
    pi = 3.14159
    def __init__(self, diameter):
        self.radius = diameter/2
        self.area = Diameter.pi * self.radius ** 2
        self.circumference = Diameter.pi * self.radius * 2

        
pizza = Diameter(12)

print(f"Pizza radius: {pizza.radius}")
print(f"Pizza area: {pizza.area}")
print(f"Pizza circumference: {pizza.circumference}")

pizza.radius = 500

print(f"Pizza radius: {pizza.radius}")
print(f"Pizza area: {pizza.area}")
print(f"Pizza circumference: {pizza.circumference}")

then you have a bug. instance properties can change, thus you can’t rely on the fact that you can instantiate a class once and that everything will stay the same, so you can’t push everything in the init method

34 Likes

Thank you, you made it very clear!

2 Likes

Hello, @stetim94

How does it works the letter “f” you added before the string, and what is its purpose?

Is it to print the object information inside de “{}”?

print(f"Pizza radius: {pizza.radius}")
print(f"Pizza area: {pizza.area}")
print(f"Pizza circumference: {pizza.circumference}")
1 Like

its an `f-string, a feature added in python3.6 if I am not mistaken

it allows us to insert variable directly inside the string, unlike .format() and % where we have to insert placeholders and then variable after the string

7 Likes

Amazing, thank you for the information.
It’s very useful!

1 Like

I love using f-strings. I really think that Codecademy should include is in the Python 3 curriculum. They are so useful.

Here is a quick, simple example.

language = 'Python'
platform = 'Codecademy'

print(f'I am learning {language} on {platform}, and I like it alot!')
7 Likes

This isn’t still clear to me, i’m sorry. :frowning:

__init__() is ideally the point where we define the initial values of our instance variables. It should not be complicated. We hand in values, they get bound to variables, and the instance is created.

Be not mislead into believing that the method creates the object. The object is created the moment we invoke the class. All we’re doing is initializing instance variables.

Should there be math functions in this module? Per the above description, no. Those should be methods that can be invoked by the instance, repeatedly.

    def area(self):
        pass

    def circumference(self):
        pass
instance = Circle(12)
print (instance.area())
print (instance.circumference())

Whatever program would accept a diameter instead of a radius has stretched to the envelope. It’s a circle. We know that all the math is based on radius. But this is pizzas, which we order by diameter. So we should initialize it that way.

def __init__(self, diameter):
    self.diameter = diameter

def radius(self):
    return self.diameter / 2

Now radius becomes a function of diameter.

def area(self):
    return self.radius() ** 2 * self.pi
6 Likes

this was very helpful. Thank you

2 Likes

I should correct myself… “all the math is based on radius” is partly true, until we look at the relation that describes a circle:

r^2 = x^2 + y^2

But no matter. It has no bearing on the point made by that post.

1 Like

Why does the exercise ask us to make an instance variable of self.radius then instead of a new method?

This exercise appears to be more a demonstration than a pragmatic example. Study the code and learn from the pattern. It shows how we can derive (compute) a virtual attribute from another fixed attribute.

1 Like

The f is used because we are printing formatted strings using {}.

while being a complete noob learning Python, f-string formatting was one of the first things i learned with the print() function, and it was a game changer for me. i wanted to add an example of concatenation to your example for comparison, for others that come across this page.

language = 'Python'
platform = 'Codecademy'

# f-string
print(f'I am learning {language} on {platform}, and I like it alot!')

# without f-string
print('I am learning ' + language + ' on ' + platform +', and I like it alot!')
1 Like