Attribute overloading and structures in classes?

F Basegmez fb at nospam.ultranet.com
Thu Aug 9 15:08:02 EDT 2001


Howdy,

I have;

class System:
    def __init__(self):
        self.cg = Cg()
        self.weight = 0

class Cg:
    def __init__(self, x = 0, y = 0, z = 0):
        self.x = x
        self.y = y
        self.z = z
    def __repr__(self):
        return str(self.x) + ", " + str(self.y) + ", " + str(self.z)

User can use this like;

>>> block=System()
>>> block.cg.x=5
>>> block.cg
5, 0, 0

Q1
Now, how can I let the user of my System class set the cg values as either

block.cg.x=5
block.cg.y=6
block.cg.z=7.5

or

block.cg = [5, 6, 7.5]  # so that block.cg[0] = block.cg.x

Q2
Is there a way to specify the cg.x, cg.y and cg.z without having to create a
Cg class first? Something similar to this:

class System:
    def __init__(self):
        self.cg.x = 0
        self.cg.y = 0
        self.cg.z = 0
        self.weight = 0







More information about the Python-list mailing list