-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.py
More file actions
23 lines (17 loc) · 864 Bytes
/
snippets.py
File metadata and controls
23 lines (17 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Python snippets
#################################
class MetaClass(type):
"""Return a class object with the __class__ attribute equal to the class object.
This allows interesting things within the class, such as __class__.__name__,
which are otherwise not available.
This class is used as a metaclass by including it in the class definition:
class SomeClass(object, metaclass=MetaClass):
Then within the class, attributes of the class can be accessed, such as
__class__.__name__. This is different from type(self).__name__,
as the latter gives the class of self, which may not be the same as the
surrounding class definition, due to inheritance.
"""
def __new__(mcs, name, bases, attrs):
attrs['__class__'] = name
return type.__new__(mcs, name, bases, attrs)
#################################