I’m looking to call a method in python from a different class like so:
class foo():
def bar(name):
return 'hello %s' % name
def hello(name):
a = foo().bar(name)
return a
Where hello(‘world’) would return ‘Hello World’. I’m aware I’ve done something wrong here, does anyone know what it is? I think it might be the way I’m handling the classes but I haven’t got my head around it yet.
In Python, non-static methods explicitly take
selfas their first argument.foo.bar()either needs to be a static method:or has to take
selfas its first argument:What happens is that in your code,
namegets interpreted as theselfparameter (which just happens to be called something else). When you callfoo().bar(name), Python tries to pass two arguments (selfandname) tofoo.bar(), but the method only takes one.