forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.init.py
More file actions
68 lines (53 loc) · 1.54 KB
/
01.init.py
File metadata and controls
68 lines (53 loc) · 1.54 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
67
68
# -*- coding: utf-8 -*-
"""
@description: 特殊方法
@author:XuMing
"""
from __future__ import print_function # 兼容python3的print写法
from __future__ import unicode_literals # 兼容python3的编码处理
# 特殊方法
# Python 使用 __ 开头的名字来定义特殊的方法和属性,它们有:
#
# __init__()
# __repr__()
# __str__()
# __call__()
# __iter__()
# __add__()
# __sub__()
# __mul__()
# __rmul__()
# __class__
# __name__
# 构造方法 __init__()
# 在产生对象之后,我们可以向对象中添加属性。
# 事实上,还可以通过构造方法,在构造对象的时候直接添加属性:
class Clothes(object):
"""
init_demo
"""
def __init__(self, color="green"):
self.color = color
my_clothes = Clothes()
print(my_clothes.color)
# 传入有参数的值:
your_clothes = Clothes('orange')
print(your_clothes.color)
# 表示方法 __repr__() 和 __str__()
class Clothes(object):
"""
repr and str demo
"""
def __init__(self, color="green"):
self.color = color
def __str__(self):
"This is a string to print."
return ("a {} clothes".format(self.color))
def __repr__(self):
"This string recreates the object."
return ("{}(color='{}')".format(self.__class__.__name__, self.color))
# __str__() 是使用 print 函数显示的结果,类似java中的toString:
my_clothes = Clothes()
print(my_clothes)
# __repr__() 返回的是不使用 print 方法的结果:
print(my_clothes.__class__, my_clothes.__class__.__name__, my_clothes.color)