0

Given I am inside a @staticmethod, how can I find the name of the current class ?

As i don't have access to self.__class__.__name__

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
philgo20
  • 6,337
  • 6
  • 34
  • 43
  • 1
    Does this help? http://stackoverflow.com/questions/3596641/how-to-get-subclass-name-from-a-static-method-in-python. – Timmy O'Mahony Nov 22 '11 at 00:59
  • yes. add it as an answer and I close it up. Once I have that name, what can I do to call some class managet 'str'.objects won't work obviously. – philgo20 Nov 22 '11 at 01:09
  • I would award you the answer as you answered first. For that you'd need to add a full answer. – philgo20 Nov 23 '11 at 17:04

2 Answers2

1

As seen in this answer How to get (sub)class name from a static method in Python?

you can change the @staticmethod to a @classmethod which takes the class as a parameter:

class Bar(object):

    @classmethod
    def bar(cls):
        print cls.__name__
        ....
Community
  • 1
  • 1
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
0

If you have control of the method (e.g. it's one you wrote), the best way is to use the @classmethod decorator instead. That will send the class as the first argument:

class Spam(object):
    @classmethod
    def eggs(cls):
        return cls.__name__
Luke Sneeringer
  • 9,270
  • 2
  • 35
  • 32