Execute function without sending 'self' to it
Posted
by
Sergey
on Stack Overflow
See other posts from Stack Overflow
or by Sergey
Published on 2011-03-15T14:38:41Z
Indexed on
2011/03/16
16:10 UTC
Read the original article
Hit count: 285
python
|google-app-engine
Is that possible to define a function without referencing to self
this way?
def myfunc(var_a,var_b)
But so that it could also get sender data, like if I defined it like this:
def myfunc(self, var_a,var_b)
That self
is always the same so it looks a little redundant here always to run a function this way: myfunc(self,'data_a','data_b')
. Then I would like to get its data in the function like this sender.fields
.
UPDATE: Here is some code to understand better what I mean. The class below is used to show a page based on Jinja2 templates engine for users to sign up.
class SignupHandler(webapp.RequestHandler):
def get(self, *args, **kwargs):
utils.render_template(self, 'signup.html')
And this code below is a render_template
that I created as wrapper to Jinja2 functions to use it more conveniently in my project:
def render_template(response, template_name, vars=dict(), is_string=False):
template_dirs = [os.path.join(root(), 'templates')]
logging.info(template_dirs[0])
env = Environment(loader=FileSystemLoader(template_dirs))
try:
template = env.get_template(template_name)
except TemplateNotFound:
raise TemplateNotFound(template_name)
content = template.render(vars)
if is_string:
return content
else:
response.response.out.write(content)
As I use this function render_template
very often in my project and usually the same way, just with different template files, I wondered if there was a way to get rid of having to call it like I do it now, with self as the first argument but still having access to that object.
© Stack Overflow or respective owner