Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 比较python中类的所有实例的属性值_Class_Python 2.7_Instance - Fatal编程技术网

Class 比较python中类的所有实例的属性值

Class 比较python中类的所有实例的属性值,class,python-2.7,instance,Class,Python 2.7,Instance,我正在尝试编写一个面向对象的程序(作为一个学习练习,我知道可能有更简单的方法),其中珠子在一个以圆环为边界的二维平面上反弹。每个珠子都是由类球定义的对象。在设置球的初始位置时,我需要检查没有其他球已经放置在相同的x和y坐标处 #Class for the beads class ball: NumbBalls = 0 #Constructor def __init__(self,Beads): self.ball = sphere(pos = vecto

我正在尝试编写一个面向对象的程序(作为一个学习练习,我知道可能有更简单的方法),其中珠子在一个以圆环为边界的二维平面上反弹。每个珠子都是由类球定义的对象。在设置球的初始位置时,我需要检查没有其他球已经放置在相同的x和y坐标处

#Class for the beads
class ball:
    NumbBalls = 0

    #Constructor
    def __init__(self,Beads):
        self.ball = sphere(pos = vector(0,0,0), radius = radiusBall,color=color.red)
        ball.NumbBalls += 1
        self.ball.pos = self.createInitialPosition(Beads)

    #Assign ball its initial position   
    def createInitialPosition(self,other):
         #CODE to compare self.ball.pos and other.ball.pos which returns a unique position coordinate


#Main program
NumbBeads  = 100
Beads = []#Create empty list for all the beads  

#create balls in random initial positions
Beads = [ball(Beads) for item in range(NumbBeads)]

如果我创建两个对象bead1和bead2,然后将bead1和bead2作为参数传递,即bead2=ball(bead1),那么我可以实现这一点。但是,如果我正在生成一个珠子列表,并且希望所有珠子都与self进行比较,那么我该怎么做呢。希望这是有意义的。

< P>也许不是你现在采取的方法,你应该考虑沿着这些线的东西(当然,对你的类定义/方法进行必要的改变):

N=100
初始位置=[]
而len(初始位置)
N = 100
initialPositions = []
while len(initialPositions) <= N:
    newPosition = generateRandomPosition()
    if newPosition in initialPositions:
        pass
    else:
        initialPositions.append(newPosition)

ballList = [ ball(p) for p in initialPositions ]