Hi. I've read the documentation but I can't quite work out how i create an object and give it attributes.

I'm trying to create a disk object that has attributes disk.x and disk.y which i can then identify with a line,

disk.id = (x,y)

Recommended Answers

All 3 Replies

Actually, after trolling through previous posts, I think this might work:

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

Yeah thats it! Well done, there are other ways to do it as well.

class disk(object):
    y = 1
    x = 2

This means you dont have to make an instance of the class

print disk.x
print disk.y
#output
#1
#2

Wait this isn't working either.
Before, I used the visual module, which had the object sphere, which I could define attributes for, such as x and y position.
I created a square array of particles like so:

alldisks = []

for x in range(0,nlat):

    for y in range(0,nlat):

        disk=sphere(pos=(x*s,y*s), radius = r, color=color.red)

        alldisks.append(disk)

        disk.id = (x,y)

then i chose a disk from the array at random using: p = choice(alldisks) Then later I could differentiate between the disk i had chosen from the otherdisks using: if disk.id != p.id I was just wondering how I could initialise these two objects without the visual module?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.