# 分形几何 # 在函数内部定义的函数 # 测试嵌套函数定义 '''def outer(): print('outer running') def inner01(): print('inner01 running') inner01() outer() def printChineseNmae(name,familyName): print('{0}{1}'.format(familyName,name)) def printEnglishNmae(name,familyName): print('{0}{1}'.format(name,familyName)) def printName(isChinese,name,familyName): def inner_print(a,b): print('{0}{1}'.format(a,b)) if isChinese: inner_print(familyName,name) else: inner_print(name,familyName) printName(True,'小七','高') printName(False,'Ivanka','Trump')''' # 测试nonlocal\global关键字的用法 def outer(): b = 10 def inner(): nonlocal b # 声明外部函数的局部变量 print('inner b:',b) b = 20 global a a = 2000 inner() print('outer b:', b) outer() print('a:', a)
# LFGB规则 #str = 'global str' def outer(): #str = 'outer' def inner(): #str = 'inner' print(str) inner() outer()
stu2 = Student s1 = Student('高淇',60) s2 = stu2('高希希',100) s1.say_score() s2.say_score()
class Student: #类名首字母大写,多个单词采用驼峰原则 company = 'SXT' #类属性 count = 0 #类属性 def __init__(self,name,score): # self必须位于第一个参数 self.name = name self.score = score Student.count = Student.count + 1 def say_score(self): print('我的公司是:',Student.company) # self必须位于第一个参数 print(self.name,'的分数是:',self.score) # 构造函数__init__() s1 = Student('高淇',60) s1.say_score() s2 = Student('高希希',100) s3 = Student('张珊', 80) print('一共创建{0}个Student对象'.format(Student.count))
# 析构方法 class Person: def __del__(self): print('销毁对象{0}'.format(self)) p1 = Person() p2 = Person() del p2 print('程序结束')
# 测试可调用方法 class SalaryAccount: '''工资计算类''' def __call__(self, salary): print('算工资啦...') yearsalary = salary*12 daysalary = salary//22.5 hoursalary = daysalary//8 return dict(yearsalary=yearsalary,monthsalary=salary,daysalary=daysalary,hoursalary=hoursalary) s = SalaryAccount() print(s(30000))