Bottom Page

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
Quote
#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())
Quote
#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.
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Hanging in View method learn_python 0 51 Feb-15-2019, 04:57 AM
Last Post: learn_python
  Why Can't I call base class function for my derived class object? AmirAB 2 71 Feb-13-2019, 03:04 PM
Last Post: snippsat
  How to resolve 404 returned from web method when not running on localhost littleGreenDude 3 92 Feb-05-2019, 09:01 PM
Last Post: littleGreenDude
  why my method doesn't find my List in the same class? Scorpio 2 82 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  How to call a method in a module using code KingPieter 4 127 Jan-15-2019, 09:13 PM
Last Post: KingPieter
  Compile c method in real time Battant 4 161 Jan-14-2019, 11:17 PM
Last Post: Gribouillis
  Calling a class inside other class dan789 9 256 Jan-06-2019, 02:00 PM
Last Post: dan789
  unable to replcae a pattern using method re.sub() Jeevananda 4 1,257 Jan-05-2019, 04:31 PM
Last Post: oneclick
  Fabric - Run method is not being finished mglowinski93 3 150 Dec-29-2018, 10:45 AM
Last Post: mglowinski93
  Magic method __str__ dan789 16 444 Dec-23-2018, 03:59 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)