Error calling variables from method class

43 Views Asked by At

Am working on a simple game/app. When I call a variable on web2py view, I get this error:

quack= duck.quack()
TypeError: unbound method quack() must be called with duck instance as first argument (got nothing instead)

my codes are here:
In the module

from gluon import *
class duck():
    def quack():
        return 'Quacks like a duck'
    def walk():
        return 'Walks like a person'

In the controller

def data_filters():
    fils = duck.quack()
    return dict(fils=fils) 

In the view:

{{extend 'layout.html'}}
{{=fils}}  
1

There are 1 best solutions below

0
On

Typically, you need to create an instance of your class if you want to use its methods.

class duck():
    def quack(self):

#...

x = duck()
fils = x.quack()

However, if the method doesn't need to refer to self or any attributes of the object, you can mark it as a static method and continue using duck.quack() as you are now.

class duck():
    @staticmethod
    def quack():
        #...