0
class A(object): def __init__(self, val): self.val = A.plus_one(val) @staticmethod def plus_one(val): return val + 1 

We can define a static method in a class and use it in any places. My question is, why do we have to namespace the static method even when we use this static method within this class, that is, to add A. before the method name.

My idea is, can we simply change the mechanism so that the static method name within the class does not have to be namespaces as in: (although Python does not support it now):

class A(object): def __init__(self, val): self.val = plus_one(val) @staticmethod def plus_one(val): return val + 1 

My intuition is, since we are calling from within the class, the priority of class members should be higher than global functions, and thus there won't be any ambiguity. Why does Python force us to namespace it?

3
  • Python doesn't force it. You can also define a "naked" function. But using a static method allows additional layer of organizing your code. Commented Apr 2, 2017 at 23:31
  • @Keith I think you misunderstood the question. It's about invoking static methods. (I was confused too.) Commented Apr 2, 2017 at 23:41
  • 3
    Three thoughts: 1) How could you otherwise call a top level method with the same name? 2) You also have to use self.member() instead of just calling member(). 3) Even static methods can be overloaded, so there's a potential difference between self.plus_one and A.plus_one. Which should be called? Commented Apr 2, 2017 at 23:46

1 Answer 1

1

Methods defined in a class are not accessible by bare name, except by code running directly in the class namespace (not inside another method). If you want to call a method, you usually have to look it up on some kind of object (either an instance, or the class itself). The different kinds of methods differ in how they behave when for different kinds of lookups.

class Test(object): def normal_method(self, foo): print("normal_method", self, foo) # none of the method names are valid here as bare names, only via self or Test try: class_method(type(self), foo) # raises NameError except NameError: pass self.class_method(foo) # this works Test.class_method(foo) # so does this @classmethod def class_method(cls, foo): print("class_method", cls, foo) # from a class method, you can call a method via the automatically provided cls cls.static_method(foo) @staticmethod def static_method(foo): print("static_method", foo) # a static method can only use the global class name to call other methods if isinstance(foo, int) and foo > 0: Test.static_method(foo - 1) # Class level code is perilous. #normal_method('x', 2) # could work in Python 3 (Python 2 checks the type of self) # but it probably won't be useful (since self is not an instance) #class_method('x', 2) # Won't work. A classmethod descriptor isn't directly callable #staticmethod(2) # Nor is a staticmethod # Top level code (these are all valid) Test().normal_method(2) # also could be Test.normal_method(Test(), 2) Test.class_method(2) # could also be Test().class_method(2) (type of the instance is used) Test.static_method(2) # could also be Test().static_method(2) (instance is ignored) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.