FAQ: Learn Python: Classes - Self


#1

This community-built FAQ covers the “Self” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Self

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I’m getting an error on submit step 2 asking if the printed result is (diameter / 2)…

class Circle:
  pi = 3.14
  def __init__(self, diameter):
   print("Creating circle with diameter {d}".format(d=diameter))
    # Add assignment for self.radius here:
    self.radius = diameter / 2

But that only works when the instance method is above the print stament and referenced in the format statement.

class Circle:
  pi = 3.14
  def __init__(self, diameter):
   self.radius = diameter / 2
   print("Creating circle with diameter {d}".format(d=self.radius))
    # Add assignment for self.radius here:

What I’m a missing here?


#4

This section was really difficult! I think it’s gonna take some time for me to really understand OOP. Can anyone point me to any good books or somewhere to practice this?

I have a question about this assignment. I’m really struggling with it.

class Circle:
  pi = 3.14
  def __init__(self, diameter):
    print("Creating circle with diameter {d}".format(d=diameter))
    self.radius = diameter / 2
  def circumference(self):
    circumference = 2 * self.pi * self.radius
    return circumference

I get that self.pi refers to the variable pi in the Class. Why is there a self.radius, but there is no variable for it? It looks like the method circumference(self) is calling it even though it was defined in init (self, diameter). Is it because init is a special constructor?


#5

In Python, it is an instance attribute constructor only, since the object exists the moment it is instantiated. Try removing the method and see if it prevents us from creating a new instance.

I can save you the trouble and tell you, no; it won’t have any bearing. A new instance can be created. It just won’t have any instance variables, only the methods and class variables it inherited.

One thing to note is that there is no record of diameter apart from its transient role in the instance intialization. Once that method has run its course, it no longer exists.

The variable that is preserved is radius, and every instance has the value set for this that was computed from the diameter when the instance was created. Because we are working with a singular code base serving multiple instances, we need to know which instance is accessing this code.

instance = Circle(10)

The instance can access the circumference method with no arguments, since self creates a binding to it and is able to access its radius attribute, 5.

instance.circumference()    # 31.4

#6

Hi,

Is the ‘radius’ of your ‘Circle’ objects half the passed in ‘diameter’ ?

yes :)! And still I can’t get past step 2. What am I doing wrong?

class Circle:
  pi = 3.14
  
  def __init__(self, diameter):
    print("Creating circle with diameter {d}".format(d=diameter))
    # Add assignment for self.radius here:
    self.radius = diameter / 2
    print(self.radius)
    
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11.46)

output:

Creating circle with diameter 12
6.0
Creating circle with diameter 36
18.0
Creating circle with diameter 11.46
5.73

What am I missing? Thanks in advance.

Regards,
Andreas


#7

sadly your workaround doesn’t work for me :(.


#8

I am having trouble understanding why in:

def circumference(self):
return 2 * self.pi * self.radius

We use self.pi instead of just pi. I understand why we use self.radius because we have defined it above but not pi.


#9

pi is a class variable:

class Circle:
  pi = 3.14 # class variable

class variables are automatically added to self.

if you intent to use pi, how is that defined within the scope of circumference method?