Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with class method
#1
Hi experts. I am new to Python but understand programming and have a fair idea about it. I need your help for following.

I am trying to execute the following code but getting an error. Both error and code provided below.

class Circle():
    pie=3.14
    def __init__(self,radius=1):
        self.radius=radius
        self.area=pie*radius*radius
        
        
    def Circum(self):
        return(2*self.pie*self.radius)

mycircle=Circle()
mycircle.Circum()     # I get error here when calling this function
Error:
TypeError: Circum() takes 0 positional arguments but 1 was given
Reply
#2
hm, that is strange. The actual error I get is
Error:
Traceback (most recent call last): File "/home/boyan/sandbox/classes/enemy.py", line 11, in <module> mycircle=Circle() File "/home/boyan/sandbox/classes/enemy.py", line 5, in __init__ self.area=pie*radius*radius NameError: name 'pie' is not defined
you can fix that by changing it to self.pie
then the code will run, but in order to see the output of Circum, you need to add print()
e.g. print(mycircle.Circum())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
As your radius is a default argument,then so can pie be.
It can also be be a class attribute with self.pie as @buran suggests.
class Circle:
    def __init__(self, radius=1, pie=3.14):
        self.radius = radius
        self.pie = pie
        self.area = self.pie * self.radius * self.radius

    def circum(self):
        return(2 * self.pie * self.rad
Use:
>>> mycircle = Circle()
>>> mycircle.circum()
6.28

>>> # Can change radius,pie is the same as it always has been 
>>> mycircle = Circle(2)
>>> mycircle.circum()
12.56
Also look at PEP-8,for small changes that i made.
Reply
#4
(Feb-10-2019, 09:39 PM)snippsat Wrote: As your radius is a default argument,then so can pie be. It can also be be a class attribute with self.pie as @buran suggests.
class Circle: def __init__(self, radius=1, pie=3.14): self.radius = radius self.pie = pie self.area = self.pie * self.radius * self.radius def circum(self): return(2 * self.pie * self.rad
Use:
>>> mycircle = Circle() >>> mycircle.circum() 6.28 >>> # Can change radius,pie is the same as it always has been >>> mycircle = Circle(2) >>> mycircle.circum() 12.56
Also look at PEP-8,for small changes that i made.

Great ... :)
Thanks a lot Bros...it feels awesome getting so much help..
Bundle of thanks both of you @buran, @snippsat
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  problem usage of static method akbarza 5 454 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Using one child class method in another child class garynewport 5 1,484 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,379 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,679 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  anonymous method in a class Skaperen 8 3,475 May-23-2021, 11:17 PM
Last Post: Skaperen
  How to apply a class method to an entire dataframe column tirtha9 1 5,051 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  NameError when calling a class method mfreudenberg 2 2,252 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020