-1

Is it possible to call a static method defined in a superclass from a method in subclass? Something like:

class A:
   @staticmethod
   def a():
      ...

class B(A):
   def b(self):
      A.a()

A.a() doesn't work, neither does B.a(), super.a() or self.a(). Is there a way to do this?

EDIT: The problem was a stale .pyc file!!!!!!

Banana
  • 4,010
  • 9
  • 33
  • 49
  • 1
    http://ideone.com/KqRtv9 : `A.a()`, `B.a()`, `self.a()`, `super(B, self).a()` work. `super().a()` will also work in Python 3.x. – falsetru Feb 11 '15 at 14:43
  • Why wouldn't `A.a()` work? That is after all, the definition of a static method. – Malik Brahimi Feb 11 '15 at 14:46
  • @MalikBrahimi, not sure, but when I try it like this I get: AttributeError: type object 'A' has no attribute 'a' – Banana Feb 11 '15 at 14:49
  • And if I try with self.a(), I get AttributeError: 'B' object has no attribute 'a' – Banana Feb 11 '15 at 14:51
  • @JanaBanana Can you again past your code with error? – Vivek Sable Feb 11 '15 at 14:58
  • Unfortunately, I cannot post the original code, but the code above is analogous. Thank you for your help, I will try to figure out what I am doing wrong. – Banana Feb 11 '15 at 18:57

2 Answers2

1

super().staticmethod() works for me.

Superclass:

class Employee:

    def __init__(self, first, last):
        self.first = first
        self.last = last

        ....

    @staticmethod
    def do_anything(strings):
        print(strings)

Subclass:

class Manager(Employee):
    
    ....
    def show_empl(self):
        
        super().do_anything("do something!")
            
aVral
  • 65
  • 1
  • 9
  • At the bottom of the question you just answered, an edit was inserted in February 2015: `EDIT: The problem was a stale .pyc file!!!!!!` – Marcus Nov 30 '22 at 21:49
  • thanks for your comments, I was not aware of that. But now I have a better understanding of [.pyc file](https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files). – aVral Dec 02 '22 at 09:06
0

Works for me - except of course for super().whatever() which only works on Python 3.x. Please explain what you mean by "doesn't work"...

Python 2.7.3 (default, Dec 18 2014, 19:10:20) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...   @staticmethod
...   def foo(what):
...       print "Foo.foo(%s)" % what
... 
>>> class Bar(Foo):
...   def bar(self):
...       self.foo("from Bar.bar")
... 
>>> b = Bar()
>>> b.bar()
Foo.foo(from Bar.bar)
>>> Foo().foo("aaa")
Foo.foo(aaa)
>>> Foo.foo("aaa")
Foo.foo(aaa)
>>> b.foo("uuu")
Foo.foo(uuu)
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • If I try with A.a() I get: AttributeError: type object 'A' has no attribute 'a' – Banana Feb 11 '15 at 14:54
  • If I try with B.a() I get: AttributeError: type object 'B' has no attribute 'a' – Banana Feb 11 '15 at 14:55
  • If I try with self.a(), I get AttributeError: 'B' object has no attribute 'a' – Banana Feb 11 '15 at 14:56
  • @JanaBanana please read the code sample I posted: it does work as expected. I also tried the code you posted (just added a print statement in `A.a`) and it does (of course) work. Your problem is elsewhere obviously, and you did NOT post a minimal executable example that reproduces your problem. – bruno desthuilliers Feb 11 '15 at 15:46
  • Unfortunately, I cannot post the original code, but the code above is analogous, or it should be. Thank you for your help, I will try to figure out what I am doing wrong. – Banana Feb 11 '15 at 18:58
  • @Banana : we don't ask you to post "the original code", but a *minimal executable example that reproduces your problem*. The code you posted does not reproduce your problem, so we cannot help you. – bruno desthuilliers Feb 25 '15 at 11:55