Python Objects and classes

class Circle:
pi = 3.14
def area(self, radius):
area = Circle.pi * radius ** 2 # area = self.pi * radius ** 2
return area

in this exercise, when i use self.pi and Circle .pi they both are giving me the same result.
what is really happening when i use self? am i referring Circle class or method

You might want to check out this discussion.

2 Likes

You’d use self when you want to assign/reference attributes of an individual instance of a class. So in your example having pi assigned to the class is perfectly valid since it should be identical for every instance. Assigning it to a named attribute of every instance is unnecessary.

Something like radius however would differ per instance and you could assign it to each instance with self.radius = radius for example. That way new Circle instances can each have their own .radius whilst sharing the same pi value which seems reasonable.

If you use self.pi it’ll go through the standard lookup order. Since the instance does not have a .pi attribute it’ll check the class attributes which in this case does have a .pi attribute so you’re effectively using the class attribute anyway. Note that generally explicit is probably better than implicit and in this example it may be best be to use Circle.pi (might not always be the case).

Definitely worth going through that discussion though.

3 Likes

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