python学习笔记(基础)
函数不定长参数
匿名函数
func=lambda x,y:x+y
rint(func(22,33))
str( )方法 把对象转化成字符串
def __str__():
return "姓名:%s 年龄%s"%(self.name,self.age)
隐藏属性-属性和方法的私有化
self.__password = pw # 不可以外部访问,只能内部访问
def __say_hello(self):
类的数据隐藏(数据私有化)
• xx :公有变量
• _xx:私有化属性或方法
• __xx:避免与子类中的属性命名冲突,无法在外部直接访问
• __xx__:用户名字空间的魔法对象或属性
• xx_:避免与python关键词冲突
为私有属性添加getter和setter方法,以访问类的私有属性
使用property升级getter和getter方法
• num = property(getNum, setNum) # num为私有属性名
• @property
@num.setter # num为私有属性名