Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Method not defined
#1
Hello,
i have been playing around with the following piece of code that i found in an online course of Python.
The class has 2 methods, one method is calling the other.
I can make it work if i use the class name in the zoo().
Currently as it is, it can execute without any errors.
If i remove the comments is providing me the following errors,according to the line in use, can someone explain me why please?
class TestClass:
	def foo(left,stop):
		pass

	def zoo(left):
		foo(left,True)
		foo(left,False)


obj1=TestClass()
obj1.foo('left')
#obj1.zoo('left') #TypeError: zoo() takes 1 positional argument but 2 were given
#obj1.zoo() #NameError: name 'foo' is not defined
Thank you
Reply
#2
Hi,

i don't know which tutorial you got the code from, but it's wrong. The 1st positional argument of methods in a class have to be self, that's by convention for self-referencing the class. The self-reference is in the end also the reason for the error you get.

I'd recommend to read through the section on class in the official Python tutorial at docs.python.org to get a better understand of how classes work and how to write them.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 3,844 May-30-2020, 04:14 AM
Last Post: DT2000
  User defined method as variable or argument AeranicusCascadia 5 4,270 Dec-13-2017, 04:47 PM
Last Post: AeranicusCascadia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020