• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Is there any reason for giving self a default value?

I was browsing through some code, and I noticed a line that caught my attention. The code is similar to the example below

class MyClass:
    def __init__(self):
        pass

    def call_me(self=''):
        print(self)

This looks like any other class that I have seen, however a str is being passed in as default value for self.

If I print out self, it behaves as normal

>>> MyClass().call_me()
<__main__.MyClass object at 0x000002A12E7CA908>

This has been bugging me and I cannot figure out why this would be used. Is there any reason to why a str instance would be passed in as a default value for self?

      • 1
    • Sounds like someone wanted to have MyClass.call_me() work too. Not a particularly good idea however, they could have made it a classmethod instead.
      • 2
    • If call_me is called from the class itself and not from an instance, then '' is passed as the instance, and the method call will not raise an error.

Not really, it's just an odd way of making it not raise an error when called via the class:

MyClass.call_me()

works fine since, even though nothing is implicitly passed as with instances, the default value for that argument is provided. If no default was provided, when called, this would of course raise the TypeError for args we all love. As to why he chose an empty string as the value, only he knows.

Bottom line, this is more confusing than it is practical. If you need to do something similar I'd advice a simple staticmethod with a default argument to achieve a similar effect.

That way you don't stump anyone reading your code (like the developer who wrote this did with you ;-):

@staticmethod
def call_me(a=''):
    print(a)

If instead you need access to class attributes you could always opt for the classmethod decorator. Both these (class and static decorators) also serve a secondary purpose of making your intent crystal clear to others reading your code.

  • 28
Reply Report

The short answer is yes. That way, you can call the function as:

MyClass.call_me()

without instantiating MyClass, that will print an empty string.

To give a longer answer, we need to look at what is going on behind the scenes.

When you create an instance of the class, __new__ and __init__ are called to create it, that is:

a = MyClass()

is roughly equivalent to:

a = MyClass.__new__(MyClass)
MyClass.__init__(a)

Whenever you use some method on a created instance a:

a.call_me()

It is "replaced" with MyClass.call_me(a).

So, having a default parameter for call_me allows you to call this function not only as a method of an instance, in which case self is an instance itself, but also as a static class method.

That way, instead of MyClass.call_me(a), just MyClass.call_me() is called. Because the argument list is empty, the default argument is assigned to self and the desired result (empty string) is printed.

  • 13
Reply Report

Trending Tags