Classes

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Thu Oct 30 16:33:01 EDT 2014


On Thursday, October 30, 2014 1:19:57 PM UTC-7, Seymore4Head wrote:
> class pet: 
>     def set_age(self,age):
>         self.age=age
>     def get_age(self):
>         return self.age
> pax=pet
> pax.set_age(4)
> 
> Traceback (most recent call last):
>   File "C:\Functions\test.py", line 18, in <module>
>     pax.set_age(4)
> TypeError: set_age() missing 1 required positional argument: 'age'
> 
> I am trying to pass 4 as the age.  Obviously I am doing it wrong.

The line `pax=pet` doesn't create an instance of your pet class, it creates essentially a copy of the class definition.

You need `pax=pet()`

Or preferably, `pax = pet()`.  The spaces are optional, but they make it more readable.



More information about the Python-list mailing list