# 这里做一个例子,用来简单说明一下
# @classmethod
# @staticmethod
# @property
# 三个@的用法
# 主要是前两个 第三个顺带下
# 假设:这里创建的类C需要处理列表形式的参数,如果给出的数据是其他类型的,
# 要求转化成列表才能使用
# 大多数人将会在使用类之前进行一系列处理然后拿着处理成列表后的参数传参创建实例
# but Python博大精深,万物皆对象的本质是尽量杜绝突兀尴尬的存在,合适的封装将
# 更适合团队协作开发,并且利于小伙伴们的理解,精神层面上讲就是有利于整体公民素质的提升...
class C:
'''
@classmethod方法使用方式:
class S()
@classmethod
def class_a(cls,other):
return cls()#随意个人习惯
这里的cls同self差不多用法,表示当前的类S
class S()
@staticmethod
def b(other):#这里没有cls和普通函数一样
return S(l)
'''
def __init__(self,list_type):
'''啥也没干,只是用来保存一个实例属性v,
要求形式是列表,
细节处理自行实现,这里只用来过度
下面的print 中的数字表示顺序0 1 2'''
print('__init__:',2)
self.cup = list_type
print('列表初始化之后的数据源:',list_type)
@classmethod
def class_a(cls,other):
print('列表初始化之前的数据源:',other)
print('@classmethod',1)
l = []
# {%这个功能 直接把本来写在外面的代码放到类里面,同时不影响类的其他任何功能%}
if type(other) == dict:
for k,v in other.items():
l.append(k)
l.append(v)
elif type(other) == int:
l.append(other)
elif type(other) == tuple or set or list or frozenset:
l+= list(other)
elif type(other) == bytes:
l += [chr(i) for i in other ]
elif type(other) == str:
l += [i for i in other]
#{%end 这个功能%}
#返回一个自类实例 但是此时的自类已经是参数改变成列表样式的新类
return cls(l)#注意这里!!!!返回的是对象哦!!被安排过得对象哦!
@staticmethod
def b(other):
'''跟上面根本就没有区别'''
print('列表初始化之前的数据源:',other)
print(' @staticmethod',1)
l = []
if type(other) == dict:
for k,v in other.items():
l.append(k)
l.append(v)
elif type(other) == int:
l.append(other)
elif type(other) == tuple or set or list or frozenset:
l+= list(other)
elif type(other) == bytes:
l += [chr(i) for i in other ]
elif type(other) == str:
l += [i for i in other]
#这里是不同处 return因为没有 cls本类参数,所以直接用C()就可以返回创建一个自类实例 不影响效果
return C(l)
def a(self):
'''用来展示成果'''
print('用列表转化器转化后的数据:',self.cup)
@property
def value(self):
return self.cup
@value.setter#value.的时候tab以下会有惊喜 setter只是沧海之一粟
def value(self,value):
if len(value) > 5:
raise ValueError('长度超过5,请输入长度小于5的参数')
if len(value) <= 5:
print('your setting is good with:',value)
self.cup = value```
# 按照你喜欢的造
t0 = [1,2,3,4,5,6]
t1 = (1,2,3,4,5,6)
t2 = set(t1)
t3 = dict(zip(t1,t2))
t4 = tuple(t1)
t5 = '1,2,3,4,5,6'
t6 = str(t1)
# 分别调用staticmethod下的和 classmethod下的 函数内容几乎一样
# 只有return不同
tclassmethod = C.class_a(t2)
tclassmethod.a()
tclassmethod.value = t4[:5]
print('\t')
lstatickmethod = C.b(t2)
lstatickmethod.a()
lstatickmethod.value = t4[:]
-----------------------------------------------------------------
结果如下:
列表初始化之前的数据源: {1, 2, 3, 4, 5, 6}
@classmethod 1
__init__: 2
列表初始化之后的数据源: [1, 2, 3, 4, 5, 6]
用列表转化器转化后的数据: [1, 2, 3, 4, 5, 6]
your setting is good with: (1, 2, 3, 4, 5)
列表初始化之前的数据源: {1, 2, 3, 4, 5, 6}
@staticmethod 1
__init__: 2
列表初始化之后的数据源: [1, 2, 3, 4, 5, 6]
用列表转化器转化后的数据: [1, 2, 3, 4, 5, 6]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ipython-input-16-2757fecf914d in module()
117 lstatickmethod = C.b(t2)
118 lstatickmethod.a()
--> 119 lstatickmethod.value = t4
120
ipython-input-16-2757fecf914d in value(self, value)
94 # 被value.setter的函数可以直接用C().value()
95 if len(value) > 5:
---> 96 raise ValueError('长度超过5,请输入长度小于5的参数')
97 if len(value) <= 5:
98 print('your setting is good with:',value)
ValueError: 长度超过5,请输入长度小于5的参数
自己整一个试试吧!