forked from ramwin/python-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_new.py
More file actions
48 lines (35 loc) · 980 Bytes
/
class_new.py
File metadata and controls
48 lines (35 loc) · 980 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Xiang Wang @ 2020-04-13 13:50:29
class Animal(object):
<<<<<<< HEAD
class_attr = 'animal'
=======
class_attr = 'class_attr'
>>>>>>> 745286e788c3e44108ec9944b26bad26314f3fa2
def __init__(self, *args, **kwargs):
print("chushihua")
pass
class Dog(Animal):
def __new__(cls, *args, **kwargs):
return super().__new__(cls, *args, **kwargs)
def say(self):
print("i'm dog")
class Cat(Animal):
def say(self):
print("I'm cat")
class Some(object):
def __new__(self, type, *args, **kwargs):
import ipdb
ipdb.set_trace()
if type == 'dog':
return Dog(*args, **kwargs)
return Cat(*args, **kwargs)
d = Some("dog")
# d.say()
d.class_attr = 'new attr' # 如果没有这个赋值,那么d的class_attr会被下面这句覆盖
Animal.class_attr = 'new attr2'
print(d.class_attr)
c = Some("cat")
# c.say()
print(c.class_attr)