Re: [Tutor] class newbie
thanks! On Sun, Jul 23, 2017 at 5:35 PM, Danny Yoowrote: > On Sun, Jul 23, 2017 at 1:24 PM, Michael C > wrote: > > class mahschool: > > def print(): > > print('Say something') > > > By the way, you've chosen a name for your method that's spelled the > same as the name of the built-in "print" function. I'd recommend you > choose a different name than "print" in your method name, just to > avoid any potential confusion. This isn't going to solve the > immediate problem that you encountered and solved: you figured out > that methods need to have a self argument. But you probably still > want to rename to avoid the name collision. > > > Good luck! > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class newbie
On 07/23/2017 02:42 PM, Michael C wrote: > never mind, I forgot to put 'self' in the method definition! class mahschool: def print(self): print('Say something') a = mahschool() a.print() Indeed. The error message was clear on this - but not in a way that's always instructive until you're used to it :) "TypeError: print() takes 0 positional arguments but 1 was given" A method is called "silently" (you didn't pass it yourself as an argument when you called print()) with the instance, so you need to declare such a parameter in the method definition. And to give myself an excuse for preaching: it's usually not a great idea to reuse the name of a built-in function. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class newbie
On Sun, Jul 23, 2017 at 1:24 PM, Michael Cwrote: > class mahschool: > def print(): > print('Say something') By the way, you've chosen a name for your method that's spelled the same as the name of the built-in "print" function. I'd recommend you choose a different name than "print" in your method name, just to avoid any potential confusion. This isn't going to solve the immediate problem that you encountered and solved: you figured out that methods need to have a self argument. But you probably still want to rename to avoid the name collision. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class newbie
never mind, I forgot to put 'self' in the method definition! class mahschool: def print(self): print('Say something') a = mahschool() a.print() On Sun, Jul 23, 2017 at 1:24 PM, Michael Cwrote: > class mahschool: > def print(): > print('Say something') > > > a = mahschool() > > a.print() > > > > With this, I get this error: > > Traceback (most recent call last): > File "test.py", line 8, in > a.print() > TypeError: print() takes 0 positional arguments but 1 was given > > > What did I do wrong? > > Thanks! > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor