# Why does circumference require ()?

Why here need a () when I wrote print(medium_pizza.circumference())?
And since def init return none, why when I test print(medium_pizza.radius), it did return radius value? and why in there I don’t need a () after radius?

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

medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza.circumference())
print(teaching_table.circumference())
print(round_room.circumference())
``````

calling functions and methods require parentheses

init is a magic method, its automatically called when you create a new instance of a class. Within the init method, you can then declare the instance variable(s)

radius is instance variable, accessing variable, instance variable and class variables doesn’t require parentheses.

28 Likes

why in the test we wort the answer for Q 3 before Q 2 and when i answer 2 first it give me error that i need to define medium_pizza and i already did so i don’t understand this part

Q2 and Q3 are question 2 and question 3? If so, please share the exercise url, so i can have a look at the instructions of the lesson

Your code is correct, what is the problem? Of course to call a method on a class instance, you first need to create the class instance

Question 2 asks you to Define three `Circle` s with three different diameters.

• A medium pizza, `medium_pizza` , that is 12 inches across.
• Your teaching table, `teaching_table` , which is 36 inches across.
• The Round Room auditorium, `round_room` , which is 11,460 inches across.

3.

Define a new method `circumference` for your circle object that takes only one argument, `self` , and returns the circumference of a circle with the given radius by this formula:

circumference = 2 * pi * radius

What you see in that link is what you get when you click “View Solution.” Question 2 asks you to declare the diameters of the different objects, whereas Question 3 asks you to set up the .circumference method. However, when you do it in that order, you get an error, and even the answer key shows that you should do the step outlined in Question 3 before Question 2. The text should probably be flipped for those two questions.

Why do you get an error? The class is defined/declared, so creating instance of the class (question 2) should work without a problem.

I’m really not sure and I wish I had copied it when it happened. This has happened a couple times in the Classes lessons where I put in something that returned an error only to find nothing different in the answer key.

1 Like
``````circumference = 2 * pi * radius
``````

That is not a method, but a statement.

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

The above depends upon the class having a `radius` attribute and a defined value for `pi`.

The same thing happened to me. My code was correct when I compared it to the solution; however, I also had to put the block of code defining the diameters of the three objects from step 2 after the block defining the circumference method from step 3. I could not get it to run without error without switching these. Once you move the method above the objects, it runs fine, but they are not written in the editor in the same order the instructions are given.

Can someone please advise if there is a certain order code must be written within the class? i.g. the init statement, then methods, then objects, then print statements. Or can a method be defined after objects are declared? Does it matter?

Consider the following,

``````>>> class Foo:
def __init__(self, name):
self.name = name

>>> def get_name(self):
return self.name

>>> Foo.get_name = get_name
>>> foo = Foo('bar')
>>> foo.get_name()
'bar'
>>>
``````

That can be quite revealing. It suggests that we may extend a class with a global function. The only problem I can foresee with this is that if `get_name()` gets changed, it will directly affect the method we added to the class.

Cancel that…

``````>>> def get_name(self):
return f"Name: {self.name}"

>>> foo.get_name()
'bar'
>>>
``````

We can even delete the global function without affecting any existing instances.

``````>>> del(get_name)
>>> id(get_name)
Traceback (most recent call last):
File "<pyshell#89>", line 1, in <module>
id(get_name)
NameError: name 'get_name' is not defined
>>> foo.get_name()
'bar'
>>>
``````

That includes for new instances; witness,

``````>>> bar = Foo('foo')
>>> bar.get_name()
'foo'
>>>
``````

As to the order we write the methods in the class it will take some more experimentation and documentation research. This is something that could well be explored at a later time so as not to disrupt your progress.

Extra Study

More rabbit hole stuff…

``````>>> Foo.id = 2
>>> foo.id
2
>>> bar.id
2
>>> def set_id(self):
Foo.id += 1
self.id = Foo.id

>>> Foo.set_id = set_id
>>> faz.id
2
>>> faz.set_id()
>>> faz.id
3
>>> set_id(faz)
>>> faz.id
4
>>> del(set_id)
>>> set_id(faz)
Traceback (most recent call last):
File "<pyshell#115>", line 1, in <module>
set_id(faz)
NameError: name 'set_id' is not defined
>>> faz.set_id()
>>> faz.id
5
>>> foo.set_id()
>>> bar.set_id()
>>> foo.id
6
>>> bar.id
7
>>>
``````
1 Like

Same thing happened to me.
Struggled to sovle it but gave up.
I just placed it with a soulution.
Still do not know why same code that I wrote does not apply.

1 Like

Impossible to say without seeing your code.

## What I wrote is below.

``````class Circle:

pi = 3.14

def __init__(self, diameter):

print("Creating circle with diameter {d}".format(d=diameter))

def circumference(self):

return 2 * self.pi * self.radius

medium_pizza = Circle(12)

teaching_table = Circle(36)

round_room = Circle(11460)
``````

The error message says - “Traceback (most recent call last):
File “script.py”, line 1, in
class Circle:
File “script.py”, line 9, in Circle
medium_pizza = Circle(12)
NameError: name ‘Circle’ is not defined”

1 Like

Hello!

When sharing code here, please make sure you format it accordingly, this is especially important for languages like Python that are so sensitive to needing the correct indentation/formatting etc as the forums software will not keep any of this if you just copy-paste it in normally:

Having tested it, the issue seems to be that the three statements at the bottom where you instantiate the class are indented by various degrees, so the Python interpreter sees them as being inside of the class, and therefore it doesn’t work, try removing the indentation for those three lines, so they lie outside of the class.

Happy coding!

1 Like

Oh! The indentation has caused the issue!
I followed your suggenstion and it worked!