Mar-02-2019, 06:47 PM
It's not class method vs. instance method. The get_size method is an instance method in both cases. And you don't call it from any instances. So it's an unbound instance method, which you have to explicitly provide an instance to. That's what you do in the first example (Pizza(4555) is the instance you are providing). In the second example you only provide an integer, and it is expecting an instance and an integer.
What you want to do is call it from an instance, so it is a bound method, and the instance parameter is provided implicitly.
What you want to do is call it from an instance, so it is a bound method, and the instance parameter is provided implicitly.
class Pizza: def __init__(self, size): self.size = size def get_size(self): return "Size={}".format(self.size) za = Pizza(4555) print(za.get_size())
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures