1

I'm implementing a meta class as follows:

from abc import ABCMeta

class Algorithm(metaclass=ABCMeta):
    # lots of @abstractmethods

    # Non-abstract method
    @property
    def name(self):
        ''' Name of the algorithm '''
        return self.__class__.__name__

class MyLittleAlgorithm(Algorithm):        
    def magic(self):
        return self.name

Then, I'm under the impression that the following two prints ought to give the same result.

a = MyLittleAlgorithm()
print(a.magic)
print(a.name)

but in fact I get:

>>> print(a.magic)
<bound method MyLittleAlgorithm.magic of <__main__.MyLittleAlgorithm object at 0x11242f438>>
>>> print(a.name)
MyLittleAlgorithm

The behaviour I'm after is the latter one. (I want every algorithm which inherits from Algorithm to have a .name @property which it can use to refer to itself.)

Please note that I do not want to instantiate the meta class. I merely want the instances of the children of it to have the .name @property without me having to write out the code for each one explicitly.

MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26

0 Answers0