[Tutor] Class Construction question

Don Arnold darnold02@sprynet.com
Mon Nov 11 19:54:02 2002


> Hey everyone,
>
> Well that copy and pasting issue was just that, a copy and paste issue.
> The code i was working with didn't have the shebang and i was doing
> ./Person.py, which was the first problem, and the second problem is with
> arguments.
>
> Traceback (most recent call last):
>   File "./Person.py", line 19, in ?
>     testing = Person("Chris")
>   File "./Person.py", line 5, in __init__
>     self.setPhone(Name)
> TypeError: setPhone() takes exactly 1 argument (2 given)
>
> Here is the modified code:
>
> #!/usr/bin/python
> class Person:
>     def __init__(Name):
>         self.setPhone(Name)
>     def setPhone(Name):
>         self.PhoneNumber = string
>     def setName(string):
> self.FirstName = string
>     def getName():
>         return self.FirstName
>     def getPhoneNumber():
>         return self.PhoneNumber

When it invokes a class method, Python passes an invisible first argument
(commonly called 'self') for you that is a reference to the current object.
You'll need to account for this in the parameter list for each method:

class Person:
    def __init__(self, Name):
        self.setPhone(Name)
    def setPhone(self, Name):
        self.PhoneNumber = string

etc.

> testing = Person("Chris")
> print testing.getName()
> testing.setName("testing")
> print testing.getName()
>
>
> Sorry for being such a pain, just i am trying to learn classes the
> python way and my C++ skills are getting in the way. Anyone know good
> class creation documentation for python. I have yet to find any real
> good ones.
>
> Thank you again everyone,
> Chris
>

HTH,

Don