Execute a function without sending an "I" to it

Is it possible to define a function without reference to selfthis?

def myfunc(var_a,var_b)

But so that he can also get the sender data, for example, if I defined it like this:

def myfunc(self, var_a,var_b)

What selfis always the same, so there is always a little over-run this function: myfunc(self,'data_a','data_b'). Then I would like to get its data in a function like this sender.fields.

UPDATE: Here is some code to better understand what I mean. The following class is used to display a page based on the Jinja2 template engine for subscribers.

class SignupHandler(webapp.RequestHandler):
    def get(self, *args, **kwargs):
        utils.render_template(self, 'signup.html')

And this code below render_template, which I created as a wrapper for 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)

render_template , , , , , , , self , .

+3
4

"", staticmethod, , :

class Foo(object):
    @staticmethod
    def foo(bar, baz, qux):
        pass

, , , :

class Foo(object):
    global_cache={}
    @classmethod
    def set(cls, key, value):
        cls.global_cache[key] = value

, , , - self . Zen of Python , " , ", self .

+9

, , ( self), staticmethod.

class Person:

  @staticmethod
  def barbar(arg1, arg2, arg3):
    # Has no idea of the class or instance in which this was called on.
    pass

, - :

Person.barbar("hi","bye","jump")
+2

"", , , , . , , s , ++, this, self


Update0

Oh wait, you are not defining an object method. Perhaps it should be, then you start object.func(*args, **kwargs), not func(object, *args, **kwargs). I need to see what you are trying to do to find out what you need.

+1
source

It looks like you should create your own subclass webapp.RequestHandlerand define your method render_templateon it:

class MyRequestHandler(webapp.RequestHandler):
    def render_template(self, template_name, vars=dict(), is_string=False):
        # Body of render_template, with "request" replaced with "self" ...

Then you use it like this in your views:

class SignupHandler(MyRequestHandler):
    def get(self, *args, **kwargs):
        self.render_template('signup.html')
+1
source

All Articles