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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically create objects 1

Status
Not open for further replies.

plip1978

Technical User
Aug 22, 2007
29
GB
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!
 
Hi

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

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
Thanks,
so just to clarify, I should return hairsList after the for loop?
 
Hi

plip1978 said:
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 [tt]__init__()[/tt] method - so have self.totalHairs set befor calling the getHairs() method which uses that property.
Code:
[b]class[/b] Monkey[teal]:[/teal]
    hairsList [teal]=[/teal] [teal][][/teal]
    totalHairs [teal]=[/teal] [COLOR=darkgoldenrod]int[/color][teal]([/teal][purple]0[/purple][teal])[/teal]

    [b]def[/b] [COLOR=darkgoldenrod][u]init[/u][/color][teal]([/teal]self[teal],[/teal] totalHairs[teal]):[/teal]
        self[teal].[/teal]totalHairs [teal]=[/teal] totalHairs
        self[teal].[/teal][COLOR=darkgoldenrod]getHairs[/color][teal]()[/teal]

    [b]def[/b] [COLOR=darkgoldenrod]getHairs[/color][teal]([/teal]self[teal]):[/teal]
        [b]for[/b] i [b]in[/b] [COLOR=darkgoldenrod]xrange[/color] [teal]([/teal][purple]0[/purple][teal],[/teal] self[teal].[/teal]totalHairs[teal]):[/teal]
            self[teal].[/teal]hairsList[teal].[/teal][COLOR=darkgoldenrod]append[/color][teal]([/teal][COLOR=darkgoldenrod]Hair[/color][teal]([/teal]i[teal]))[/teal]
        [b]return[/b] self[teal].[/teal]hairsList


Feherke.
 
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!
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top