0

I have a class like this:

class Eggs:

    def __init__(self):
        pass

    def Spam(self):
        print "spamming object"

    @staticmethod
    def Spam():
        print "spamming class"

The problem I have is that when I create an object of the Eggs class, call it x and call x.Spam() I get the output spamming class instead of spamming object. I'm not sure why this is happening because the definition was pretty clear. I've looked through Python's docs and this website but haven't found any fixes. Anyone have any ideas?

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • There's only one method called `Spam`, which gets called regardless of the type of the object (instance or class). – vaultah Aug 26 '16 at 20:14
  • last defined method wins. Others are masked. no matter the number of arguments. – Jean-François Fabre Aug 26 '16 at 20:17
  • 1
    Note that `staticmethod` in Python != static in other languages. There is just **one** namespace for class attributes, and this same namespace is used when looking up attributes on instances when the attribute doesn't exist on that instance. There is no distiction between different 'types' for a given name here. – Martijn Pieters Aug 26 '16 at 20:18
  • I would look at [this question](http://stackoverflow.com/questions/13094242/in-python-decorate-two-methods-with-the-same-name-to-distinguish-them) for help here. Basically, the last definition of a method that has the same name as other methods that come before it will get used because you can't have duplicate keys in the dictionary that Python uses to hold all method names. I don't think that this question deserves the downvotes that it's gotten. – gr1zzly be4r Aug 26 '16 at 20:19
  • For everyone who commented, thanks. I was assuming that `@staticmethod` worked the same as declaring a method `static` would in other languages. Just shows what happens when you assume I guess – Woody1193 Aug 29 '16 at 19:21

0 Answers0