-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmeta_class.py
More file actions
68 lines (52 loc) · 1.48 KB
/
meta_class.py
File metadata and controls
68 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
import weakref
# class
class MyClass(object):
def __new__(cls):
print('{0}.__new__()'.format(cls.__name__))
obj = object.__new__(cls)
return obj
def __init__(self):
print('{0}.__init__()'.format(self.__class__))
class SubClass(object):
pass
# function
def MyFun():
print("shit")
def gen_MyFun():
print("shit")
def inner():
print("damn")
return inner
# danamic class
myclass = type("myclass", (object, ), {"__init__":lambda self:None})
# equal
class myclass(object):
def __init__(self):
pass
def gen_class():
birth_hash = []
def __new__(cls):
obj = object.__new__(cls)
cls.save_birth_hash(obj)
return obj
def __init__(self):
pass
def method(self):
print(self.__class__)
@classmethod
def saybefore_create(cls):
print("hi", cls)
@classmethod
def save_birth_hash(cls, obj):
obj_ref = weakref.ref(obj)
birth_hash.append(obj_ref)
@classmethod
def get_birth_hash(cls):
return birth_hash
return type("MyClass", (object, ), {"__new__":__new__,
"__init__":__init__,
"method":method,
"saybefore_create":saybefore_create,
"save_birth_hash":save_birth_hash,
"get_birth_hash":get_birth_hash})