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:
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
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
__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.
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.
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.
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!')