简体   繁体   中英

Object has no attribute in python

I am writing a code in pythony, where I want my ball to move on a graph, and save the positions in a list (posx and posy) I made a ball class, but when I call the constructor, I get this error message: 'Ball' object has no attribute 'posx'. This is my code:

class Ball:
    def __init__(self,xvelocity=1, yvelocity=1, inx=1,iny=5, posx=[],posy=[]):
        self.xvelocity=xvelocity
        self.yvelocity=yvelocity
        #giving velocity
        self.inx=inx
        self.iny=iny
        #defining starting position
        self.posx[0]=self.inx
        self.posy[0]=self.iny

        


ball=Ball()

Do you know why this happens? Thank you for your help.

You have to define the attributes posx and posy as lists first. The next question would be if you actually want the position lists passed as argument.

    self.posx = [self.inx]
    self.posy = [self.iny]

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