Friday, 15 May 2015

python - Get class name in attribute definition -



python - Get class name in attribute definition -

i need create mixin knows name of class using it. sort of like:

class foomixin(...): bar = self.__class__

except self not defined @ attribute definition time. there clean way accomplish it's transparent class inherits mixin?

at time of definition of mixin, nobody knows in classes mixin used. can name dynamically in class methods using self.__class__.__name__:

class foomixin(object): def some_method(self): print "i'm in class %s" % self.__class__.__name__ class main(foomixin): pass instance = main() instance.some_method() # "i'm in class main"

python

No comments:

Post a Comment