×
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

Dynamically create objects

Dynamically create objects

Dynamically create objects

(OP)
Hello,
I am new to Python programming so please excuse what may be a simple question:

I have created a class which contains a list of other classes, e.g.

class Monkey:
    hairsList = []
    totalHairs = int(0)

    def __init__(self, totalHairs):
        self.hairsList = self.getHairs()
        self.totalHairs = totalHairs

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

class Hair:
    hairNumber = (0)

    def __init__(self, hairNum):
        self.hairNumber = hairNum



I then want to be able to access individual properties of specific classes.  I create a Monkey instance and attempt to access the individual parts of its hair, e.g.

ape= Monkey(2000)
print ape.hairsList[50]

However, I am getting an error: 'TypeError: 'NoneType' object is not subscriptable'.

Is there a way of accessing objects in this manner?  Am I approaching this problem in completely the wrong way?

Thanks in advance for any help!

RE: Dynamically create objects

Hi

You assigned the value returned by the getHairs() method to hairsList. While getHairs() has no return statement, it implicitly returns None. So that will be in hairsList.

Next time please post your code between [code] and [/code] TGML tags.

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

RE: Dynamically create objects

(OP)
Thanks,
so just to clarify, I should return hairsList after the for loop?

RE: Dynamically create objects

Hi

Quote (plip1978):

I should return hairsList after the for loop?
Yes, you could do it so you can chain the getHairs() calls in the future.
Yes, you should do it, for semantic reasons - methods having name starting with "get" usually return something.

Beside that, I would change the call and remove the assignment. As it sets up its own property, there is no much reason to make the assignment again.

Another important thing, is to reverse the lines in the __init__() method - so have self.totalHairs set befor calling the getHairs() method which uses that property.

CODE

class Monkey:
    hairsList = []
    totalHairs = int(0)

    def init(self, totalHairs):
        self.totalHairs = totalHairs
        self.getHairs()

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

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

RE: Dynamically create objects

also getHairs could be replaced with a simple list comprehension

CODE

self.hairsList=[hair(x) for x in xrange(self.totalHairs)]

I do not Have A.D.D. im just easily, Hey look a Squirrel!

RE: Dynamically create objects

(OP)
Yes, I had those two line reversed in the original code, but when I tried to rewrite a simpler one for illustrative purposes I put them in the wrong order!

Thanks for all your advice!

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