Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute function without sending 'self' to it

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.

like image 996
Sergei Basharov Avatar asked Aug 06 '26 23:08

Sergei Basharov


2 Answers

If you don't need self, you can use the staticmethod decorator to create a method which does not receive the object as its first argument:

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

If you're writing code which deals only with class-global data, by contrast, you can use a class method:

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

If, by contrast, you're writing code which really does need to refer to the object instance -- self is part of the language. Part of the Zen of Python is that "Explicit is better than implicit", and the use of self is an example of this.

like image 117
Charles Duffy Avatar answered Aug 08 '26 13:08

Charles Duffy


To call a function that exists in a class, but does not need to know anything about itself (hense removing the self argument), you can use the staticmethod.

An example of this might be

class Person:

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

To call this, you would do something like:

Person.barbar("hi","bye","jump")
like image 41
Mike Lewis Avatar answered Aug 08 '26 13:08

Mike Lewis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!