Say I have a class Bucket
and a function do_thing
(this is a hugely simplified example). I want to pass an argument to do_thing
that will create an instance of Bucket
and carry out the specified method on that Bucket
object.
Class Bucket:
__init__(self, volume):
...
def fill(self):
...
def empty(self):
...
def do_thing(method, vol):
A = Bucket(vol)
A.method()
This doesn't work, as obviously if I try do_thing("fill", 200)
, it raises AttributeError: Bucket instance has no attribute 'method'
. So how can I call the specific method (fill
in this case?)