×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

accessing class objects of class objects

accessing class objects of class objects

accessing class objects of class objects

(OP)
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.
 

RE: accessing class objects of class objects

Hi

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

Feherke.
http://free.rootshell.be/~feherke/

RE: accessing class objects of class objects

(OP)
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[i]. it always calls monkeyList[0]...
Thanks!
 

RE: accessing class objects of class objects

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 --> output

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.
http://free.rootshell.be/~feherke/

RE: accessing class objects of class objects

(OP)
So classProperty is an attribute that all instances of a particular class share?  While an instance property is specific to that instance?
Thanks again.

RE: accessing class objects of class objects

(OP)
Sorry, just re-read your post, you say that at the top of your post already!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close