Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing class objects of class objects

Status
Not open for further replies.

plip1978

Technical User
Aug 22, 2007
29
GB
Hello,
I have a class which contains a list:
Code:
class Monkey:
    __hairsList = []
    __totalHairs = int(0)
    __monkeyNum = int(0)

    def init(self, monkeyNum, totalHairs):
        self.__monkeyNum = monkeyNum
        self.__totalHairs = totalHairs
        self.getInitialHairs()

    def getInitialHairs(self):
        for i in xrange (0, self.totalHairs):
            self.hairsList.append(Hair(i))
        return self.hairsList

    def getHairs(self):
        return self.__hairsList
this class is contained within another class, so that when the program is initially run, it builds the class object, and also a list of Monkey class objects, i.e.:
Code:
class Zoo:
    monkeyList = []
    __totalMonkeys = int(0)

    def init(self, totalMonkeys):
        self.__totalMonkeys = totalMonkeys
        self.getInitialMonkeys()

    def getInitialMonkeys(self):
        totalHairs = 10000
        for i in xrange (0, self.__totalMonkeys):
            self.monkeyList.append(Monkey(i, totalHairs))
        return self.monkeyList

Now my problems comes when I try to run something like this:
Code:
newArea = Zoo(53)
newArea.monkeyList[1].hairsList[0].getHairs()
because whatever number I put in the index of monkeyList, it always seems to call monkeyList[0].
I'm guessing that I am misunderstanding the way that these objects relate to each other, but I am new to Python and OOP so I can't see the wood for the trees! Apologies if this post doesn't explain my situation very well - I've tried to create a simplified example!

Thanks in advance for any advice.
 
Hi

Your code contains errors :
[ul]
[li]constructors should be calles [tt]__init__()[/tt][/li]
[li]in many places you forget the __ prefix from the property names[/li]
[li]who not followed thread278-1642461 not knows from where to take the Hair class[/li]
[li]use the getter method to access object property from outside[/li]
[/ul]
Regarding the error you noticed, I can take a look only later.

Feherke.
 
Ok, apologies for not correcting the original code, I just cut and pasted it! I have this correct in my own code anyway.
Code:
class Hairs:
    __molculesList = []
    __totalHairs = int(0)
    __hairNumber = int(0)

    def __init__(self, obj, hairNum, totHairs):
         self.obj = obj
         self.__hairNumber = hairNum
         self.__totalHairs = totHairs
         self.__moleculesList = [some list of numbers...]

    def getMolecules(self):
        return self.__moleculesList


class Monkey:
    hairsList = []
    __totalHairs = int(0)
    __monkeyNum = int(0)

    def __init__(self, obj, mkyNum):
        self.obj = obj
        self.__monkeyNum = mkyNum
        self.__totalHairs = 1000
        self.getInitialHairs()

    def getInitialHairs(self):
        for i in xrange (0, self.__totalHairs):
            self.hairsList.append(Hairs(self.obj, i, self.__totalHairs))
        return self.hairsList

class Zoo:
    monkeyList = []
    __totalMonkeys = int(0)

    def __init__(self, obj):
        self.obj = obj
        self.__totalMonkeys = 75          
        self.getInitialMonkeys()

    def getInitialMonkeys(self):
        for i in xrange (0, self.__totalMonkeys):
            self.monkeyList.append(Monkey(self.obj, i))
        return self.monkeyList

newArea = Zoo(worldOfZoos)
newArea.monkeyList[1].hairsList[0].getMolecules()

Ok, this code should be a bit better, and it looks a bit more like my example. My problem is that whatever value I put in monkeyList. it always calls monkeyList[0]...
Thanks!
 
Hi

You hit the class vs. instance property difference. The class property is the same for each instance of that class. ( In Java is called static. ) In Python you declare the class propertied outside the methods.
Code:
class Monkey():
  classProperty = []
  def __init__(self, one, two):
    self.instanceProperty = []
    self.classProperty.append(one)
    self.instanceProperty.append(two)
  def whoAreYou(self):
    print 'object %s , instanceProperty : %s , classProperty %s' % ( self, self.instanceProperty, self.classProperty )

m0 = Monkey( 1, 2 )
m1 = Monkey( 3, 4 )
m2 = Monkey( 5, 6 )

m0.whoAreYou()
m1.whoAreYou()
m2.whoAreYou()
Code:
object <__main__.Monkey instance at 0xb73a8a2c> , instanceProperty : [2] , classProperty [1, 3, 5]
object <__main__.Monkey instance at 0xb73a8a4c> , instanceProperty : [4] , classProperty [1, 3, 5]
object <__main__.Monkey instance at 0xb73a8aec> , instanceProperty : [6] , classProperty [1, 3, 5]


Feherke.
 
So classProperty is an attribute that all instances of a particular class share? While an instance property is specific to that instance?
Thanks again.
 
Sorry, just re-read your post, you say that at the top of your post already!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top