forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetaclass.py
More file actions
24 lines (16 loc) · 685 Bytes
/
metaclass.py
File metadata and controls
24 lines (16 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Gadget(type):
def __new__(self, name, bases, namespace):
print(f"Creating a {name} gadget!")
return super().__new__(self, name, bases, namespace)
@classmethod
def __prepare__(cls, name, bases):
return {'color': 'white'}
class Thingamajig(metaclass=Gadget):
def __init__(self, widget):
self.widget = widget
def frob(self):
print(f"Frobbing {self.widget}.")
thing = Thingamajig("button") # also prints "Creating Thingamajig gadget!"
thing.frob() # prints "Frobbing button"
print(Thingamajig.color) # prints "white"
print(thing.__class__) # prints "<class '__main__.Thingamajig'>"