简体   繁体   中英

Oop for class circle

Im writing a class in python called Circle. Now as part of the class I want to define methods so I did but when I run the program it crashes and says they are not defined. I cant find the problem.

class Circle():
""" Holds data on a circle in the plane """
    def __init__(self,*args):
        if isinstance(args[0],Point) and isinstance(args[1],(float,int)):
            assert args[1]>0
            self.center= args[0]
            self.r= args[1]

        else:
            assert args[2]>0
            self.a=args[0]
            self.b=args[1]
            self.center= Point(self.a,self.b)
            self.r= args[2]
     def __mul__(self,other):
         assert isinstance(other,(float,int))
         assert other>0
         return Circle(self.center,self.r*other)

     __rmul__= __mul__
    def area(self):
        return math.pi*self.r**2

    def circumference(self):
        return 2*self.r*math.pi

    def move(self,p):
        assert isinstance(p,Point)
        self.center= p
        return None

I wrote a class for Point aswell, so thats not the problem. This is what happens when I run the porgram:

>>> a=Circle(-3,-3,1)
>>> area(a)
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
area(a)
NameError: name 'area' is not defined

Edit : as @jsbueno points out, this was not the error causing your error message: Your indentation is off ( def __mul__ should be 1 space to the left), therefore Python thinks you have ended your class definition and are simply defining some more functions (not class methods).

Also, you should call area as a method - a.area() , not area(a) .

I've done a bit of reworking - added some comments, renamed some variables, generally cleaned up - this now works properly:

from math import pi

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Circle:
    """
    Holds data on a circle in the plane
    """
    def __init__(self, a, b, c=None):
        if c is None:
            # Circle(Point, scalar)
            self.center = a
            self.r = b
        else:
            # Circle(scalar, scalar, scalar)
            self.center = Point(a, b)
            self.r = c

    @property
    def r(self):
        return self._r

    @r.setter
    def r(self, new_r):
        assert new_r > 0
        self._r = new_r

    def __mul__(self, scale_by):
        return Circle(self.center, self.r * scale_by)

    __rmul__ = __mul__

    def area(self):
        return pi * self.r**2

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

    def move(self, new_center):
        self.center = new_center

then

a = Circle(-3,-3,1)
print(a.area())

gives

3.141592653589793

which is correct.

The methods in a class are available to the instance, but they have to be called as components of the instance with the "." operator.

So, in your example, you should use

a = Circle()
a.area()

and not

area(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM