Writing a class inside a function in Python
I am trying to write a class inside a function... something like the following:
def func(**args):
class BindArgs(object):
foo = args['foo']
print 'foo is ', foo
def __init__(self,args):
print " hello, I am here"
f = func(foo=2)
I was hoping that print will be executed.. but the init block is not being executed... though print 'foo is ' stub runs... What am I missing?
I am trying to understand, how does this module works (https://github.com/tweepy/tweepy/blob/master/tweepy/binder.py)?
You just need to return an instance of the class...
def func(**args):
class BindArgs(object):
foo = args['foo']
print 'foo is ', foo
def __init__(self,args):
print " hello i am here"
return BindArgs(args) #return an instance of the class
f = func(foo=3)
If you wanted to call it later like in the example from the comments, you could just do
def func(**args):
class BindArgs(object):
foo = args['foo']
print 'foo is ', foo
def __init__(self,args):
print " hello i am here"
return BindArgs
f = func(foo=3)
f(args="yellow")