Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with class method
#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


Messages In This Thread
problem with class method - by AmirAB - Feb-10-2019, 08:25 PM
RE: problem with class method - by buran - Feb-10-2019, 08:30 PM
RE: problem with class method - by snippsat - Feb-10-2019, 09:39 PM
RE: problem with class method - by AmirAB - Feb-13-2019, 01:51 AM

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 455 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,381 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,680 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,478 May-23-2021, 11:17 PM
Last Post: Skaperen
  How to apply a class method to an entire dataframe column tirtha9 1 5,052 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  NameError when calling a class method mfreudenberg 2 2,254 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