一 实例变量与类变量
class Pepple:
__age=18
__name="zhangfff"
@classmethod
def GetInfo(cls):
print(cls.__name)
AAA=Pepple
AAA.GetInfo()
以上代码 利用类方法输出类变量
print(AAA.__name)
如果直接这样输出会报错,因为__开头的类变量收到保护
但是如果这样输出!!!!
class Pepple:
__age=18
__name="zhangfff"
@classmethod
def GetInfo(cls):
print(cls.__name)
AAA=Pepple
print(AAA._Pepple__name)
会得到 正确的结果, python对私有变量的保护是假的 会将其改名 为 _类名__XXXX 实例的
__name 改成了 _Pepple__name __age ---> _Pepple__age

class Pepple:
__age=18
__name="zhangfff"
@classmethod
def GetInfo(cls):
print(cls.__name)
AAA=Pepple
AAA.__age=20
print(AAA.__age)print(AAA._Pepple__age)
这里需要注意 这里虽然可以赋值 但是这个变量 AAA.__age 其实是实例变量 不是类变量

另外 以_一个下划线开头的 建议 也不要再方法外部直接访问(这种可以直接访问)
二 _ 下划线使用
for _ in range(1,10):
print(_)
一般再不用 _的时候 也就是 可以用_ 代替一般的i
三 装饰器简单理解
class Test:
# @property
def salary(self):
print("salary.aaa")
return 1000
AAA=Test()
print(AAA.salary())
# print(AAA.salary)

利用装饰器之后
class Test:
@property
def salary(self):
print("salary.aaa")
return 1000
AAA=Test()
# print(AAA.salary())
print(AAA.salary)

四 python3的魔法方法
class Foo:
"""
this is the dockmet
"""
def func(self):
pass
print(Foo.__doc__)

五 python 推导式
lis = [x*x for x in range(10)] print(lis)
lis = [x*x for x in range(10) if x %2 ==0 ] print(lis)
lis = [a+b for a in '123' for b in 'abc'] print(lis)
dic={"k1":"v1","k2":"v2"}
a = [k+ ":" +v for k,v in dic.items()]
print(a)
dic={i:i**3 for i in range(5)}
print(dic)
s={i for i in "abcdasd" if i not in "abc"}
print(s)
结果为集合