class Custom(type):
@classmethod
def __getitem__(cls, item):
raise NotImplementedError("")
@classmethod
def test(cls):
print("class custom : test")
class Book(metaclass=Custom):
Note = 0
Pad = 1
Name = { Note : "Note", Pad : "Pad"}
@classmethod
def __getitem__(cls, item):
return Book.Name[item]
@classmethod
def test(cls):
print("class book: test")
My intention is to have
Book[Book.Note] returns "Note"
It seems __getitem__() is not overrideable, unlike test(). How do I make it work ?
You're using a metaclass here. This isn't strictly inheritance: you've defined the class of the class Book
as Custom
, whereas it used to be type
. Because magic methods like __getitem__
are looked up directly on the class, and not on the instance, indexing Book[whatever]
will actually call the __getitem__
method of the class of Book
, which happens to be Custom
.
My intention is to have
Book[Book.Note]
returns "Note"
In that case, you should make the class of Book
implement __getitem__
such that it returns "Note"
. Since the class of Book
is Custom
, this is where the change needs to be made:
class Custom(type):
def __getitem__(cls, item):
return cls.Name[item]
...
class Book(metaclass=Custom):
... # as is, although you don't need the @classmethod __getitem__
Book[Book.Note] # "Note"
Book[1] # "Pad"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With