[Tutor] a class knowing its self

Ewald Ertl ewald.ertl at hartter.com
Thu Jan 19 12:09:11 CET 2006


Hi!


Ben Vinger wrote:
> Hello
> 
> I've been reading about how a class has access to its
> own 'self', so I tried the following, but it is not
> working as I would expect:
> 
> class Skill:
>    def __init__(self):
>       self.history = []      
> 
>    def setName(self, skill):
>       self.name = skill
> 
>    def getName(self):
>       return self.name
> 
> # Assigning data to my class:          
> 
> SkillNames = [r'python', r'apache', r'mysql']
> 
> #a.)
> python = Skill()
> python.setName('python')
> print python.getName()
> 
> #b.)
> for s in SkillNames:
>   s = Skill()
>   s.setName(s)
>   print s.getName()
> 
Have a more precisely look at your code.
s.getName() does just return, what you have put into
the class with setName(). ( Reassignment of a variable to
something different what you intended ).

HTH Ewald


> Why does a work and b not?   
> 
> b returns:
> <__main__.Skill instance at 0x401e260c>
> <__main__.Skill instance at 0x401e230c>
> <__main__.Skill instance at 0x401e23ec>
> 
> why does b not return the names of the 3 instances of
> Skill?
> 
> Thanks
> Ben
> 
> 
> 		
> ___________________________________________________________ 
> To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list