python3基础04 异常,函数,类

mac2024-05-24  52

整理于2020年11月下旬,献给不甘平凡的你 更多python3基础知识请查收于:https://blog.csdn.net/weixin_45316122/article/details/109843899

 

Trick:纯demo,心在哪里,结果就在那里

 

# -*- coding: utf-8 -*- # Author : szy # Create Date : 2019/10/18 # 函数参数传递 # def mytest(num): # return num * 2 # # 不光可以传递变量,还可以传递函数 # def convert(func, seq): # print('convert sequence of numbers to same type') # return [func(eachNum) for eachNum in seq] # # # myseq = [123, 45.67, -6.2e8] # # 面向对象编程说白了就是把对象传来传去,对象是第一要素 # # 面向函数编程说白了就是把函数传来传去,函数是第一要素 # print(convert(int, myseq)) # print(convert(float, myseq)) # print(convert(mytest, myseq)) # print(convert(lambda x: x*2, myseq)) # 闭包 # def log(func): # # def wrapper(x): # print("in wrapper") # func(x) # return wrapper # # 装饰器 # @log # def my_deca(x): # print("hello deca") # print(x) # # # my_deca(4) """ # 生成器 有点像你熟悉的Iterator g = (num*2 for num in myseq) print(g) for i in g: print(i) # 参数的默认赋值 def taxMe(cost, rate=0.0825): return cost + cost * rate print(taxMe(100)) print(taxMe(100, 0.05)) def taxMe2(cost, rate=0.0825, *theRest): for eachRest in theRest: print('another arg:', eachRest) cost += eachRest return cost + cost * rate print(taxMe2(100, 0.05, 100, 200, 300, 400, 500, 600, 700)) print(taxMe2(100)) def taxMe3(cost, rate=0.0825, **theRest): for eachRest in theRest.keys(): print('another arg:', eachRest) cost += theRest[eachRest] return cost + cost * rate print(taxMe3(100, 0.05, electric=100, water=200, gas=300)) """ # 文档内部的数据更新 #包与目录 # -*- coding: utf-8 -*- # Author : szy # Create Date : 2019/10/18 # 函数参数传递 # def mytest(num): # return num * 2 # # 不光可以传递变量,还可以传递函数 # def convert(func, seq): # print('convert sequence of numbers to same type') # return [func(eachNum) for eachNum in seq] # # # myseq = [123, 45.67, -6.2e8] # # 面向对象编程说白了就是把对象传来传去,对象是第一要素 # # 面向函数编程说白了就是把函数传来传去,函数是第一要素 # print(convert(int, myseq)) # print(convert(float, myseq)) # print(convert(mytest, myseq)) # print(convert(lambda x: x*2, myseq)) # 闭包 # def log(func): # # def wrapper(x): # print("in wrapper") # func(x) # return wrapper # # 装饰器 # @log # def my_deca(x): # print("hello deca") # print(x) # # # my_deca(4) """ # 生成器 有点像你熟悉的Iterator g = (num*2 for num in myseq) print(g) for i in g: print(i) # 参数的默认赋值 def taxMe(cost, rate=0.0825): return cost + cost * rate print(taxMe(100)) print(taxMe(100, 0.05)) def taxMe2(cost, rate=0.0825, *theRest): for eachRest in theRest: print('another arg:', eachRest) cost += eachRest return cost + cost * rate print(taxMe2(100, 0.05, 100, 200, 300, 400, 500, 600, 700)) print(taxMe2(100)) def taxMe3(cost, rate=0.0825, **theRest): for eachRest in theRest.keys(): print('another arg:', eachRest) cost += theRest[eachRest] return cost + cost * rate print(taxMe3(100, 0.05, electric=100, water=200, gas=300)) """ # 文档内部的数据更新 #包与目录 # -*- coding: utf-8 -*- # Author : szy # Create Date : 2019/10/18 # public # private # protected # 单下划线、双下划线、头尾双下划线说明: # __foo__: 定义的是特殊方法,类似 __init__() 之类的。 # _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import * # __foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。 # # class AddrBookEntry(object): # myVersion = '1.0' # 类属性 # __slots__ = ['name', 'phone', '__extra', '_protect'] # 定义构造方法/ # 定义方法 # def updatePhone(self, new_phone): # self.phone = new_phone # print('Updated phone# for:', self.name) # # def __add__(self, other): # print(self.name + ' ' + other.name) # # def __str__(self): # return "%s to string" % self.name # def __getattribute__(self, item): # print("__getattribute__ is called() %s" % item) # return object.__getattribute__(self, item) # class Addr(AddrBookEntry): # pass # 创建实例 # john = AddrBookEntry('John', '123') # jane = AddrBookEntry('Jane', '456') # # print(john) # print(john.name, john.phone) # print(jane.name, jane.phone) # john.updatePhone("000") # print(john.name, john.phone) # print(AddrBookEntry.myVersion) # # john.gender = "male" # print(john.gender) # addr = Addr("yasaka", "186") # print(addr._protect) # print(addr.__extra) # john + jane # print(bool([None])) # print(1/2) # print(3.0/2.0) # print(1//2) # print(3.0//2.0) # # a, b = divmod(15, 6) # print(a, b) # # a = ("one", "two") # print(a[0]) # b = "just-one", # print(b[0]) # c = ("just-one") # print(c[0]) # d = "just-one" # print(d[0])

 

最新回复(0)