Is there any reason for giving self a default value?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)