[Tutor] a class knowing its self

Shuying Wang shuying at gmail.com
Thu Jan 19 01:16:31 CET 2006


Ben,

If you change example #b to:

for s in SkillNames:
   skill = Skill()
   skill.setName(s)
   print skill.getName()

You will find that the results are the same as #a. In your #b example,
you are giving setName() the skill instance instead of the string you
intended.

--Shuying

On 1/19/06, Ben Vinger <benvinger at yahoo.co.uk> wrote:
> 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()
>
> Why does a work and b not?


More information about the Tutor mailing list