python笔记四

  1. 创建一个函数:
    def functionname([parameterlist]):
    [’’‘comments’’’]
    [functionbody]

def filterchar(string):
‘’‘功能:过滤危险字符(如黑客),并将过滤后的结果输出
about:要过滤的字符串
没有返回值
‘’’
import re
pattern=r’(黑客)|(抓包)|(监听)|(Trojan)’
sub=re.sub(pattern,’@_@’,string)
print(sub)

about=‘我是一名程序员,喜欢看黑客方面的图书,想研究一下Trojan’
filterchar(about)

我是一名程序员,喜欢看@@方面的图书,想研究一下@@

空函数的定义:
def func():
pass
def func():

  1. 函数参数的传递:当实参为不可变对象时,进行的是值传递。当实参为可变对象时,进行的是引用传递。
    参数传递时参数的数量和位置必须与定义时一致,单通过关键字传递参数时,位置可以不一样。

  2. 可变参数:
    *parameter:这种形式表示接收任意多个实际参数并将其放到一个元组中。
    def printsinger(*name):
    print(’\n我喜欢的歌手有:’)
    for item in name:
    print(item,end=’ ')
    printsinger(‘Taylor Swift’,‘Sam Smith’,‘Kelly Clarkson’,‘OneRepublic’)
    param=[‘Taylor Swift’,‘Sam Smith’,‘Kelly Clarkson’,‘OneRepublic’]
    printsinger(*param)

我喜欢的歌手有:
Taylor Swift Sam Smith Kelly Clarkson OneRepublic
我喜欢的歌手有:
Taylor Swift Sam Smith Kelly Clarkson OneRepublic

**parameter:这种形式表示接收任意多个显式赋值的实际参数,并将其放到一个字典中。
def printsign(**sign):
for key,value in sign.items():
print("["+key+"}"+“的作品有:”+value)
#第一个调用的键值不能有空格,还不能有引号
printsign(TaylorSwift=‘New Romantics’,SamSmith=‘Make It To Me’,KellyClarkson=‘Piece By Piece’,OneRepublic=‘Ordinary Human’)
param={‘Taylor Swift’:‘New Romantics’,‘Sam Smith’:‘Make It To Me’,‘Kelly Clarkson’:‘Piece By Piece’,‘OneRepublic’:‘Ordinary Human’}
printsign(**param)

[TaylorSwift}的作品有:New Romantics
[SamSmith}的作品有:Make It To Me
[KellyClarkson}的作品有:Piece By Piece
[OneRepublic}的作品有:Ordinary Human
[Taylor Swift}的作品有:New Romantics
[Sam Smith}的作品有:Make It To Me
[Kelly Clarkson}的作品有:Piece By Piece
[OneRepublic}的作品有:Ordinary Human

  1. 匿名函数: result=lambda[arg1[,arg2,arg3]]:experssion
    import math import math
    r=10
    result=lambda r:math.pirr
    print(‘半径为’,r,‘的圆面积为:’,result®)

半径为 10 的圆面积为: 314.1592653589793


  1. class ClassNamw:
    ‘’‘类的帮助信息’’’
    statement
    #空类
    class Geese:
    ‘’‘大雁类’’’
    pass
    #实例化一个类
    wildGoose=Geese()
    print(wildGoose)

<main.Geese object at 0x0000008245F4D978>

init(self):想当于c++的构造函数,且self参数不可省略,相当于c++的this指针
class Geese:
‘’‘大雁类’’’
def init(self):
print(“我是大雁类!”)
#实例化一个类
wildGoose=Geese()

我是大雁类!

类的成员包括实例方法和数据成员,数据成员又分类属性和实例属性

  • 实例方法:必须包括self参数,self的实参可以省略
    class Geese:
    ‘’‘大雁’’’
    def functionName(self,beak,wing,claw):
    print("我是大雁类!我有以下特征: ")
    print(beak)
    print(wing)
    print(claw)
    beak=“喙的基部较高,长度和头部的长度几乎相等”
    wing=“翅膀长而尖”
    claw=“爪子是蹼状的”
    wildGoose=Geese()
    wildGoose.functionName(beak,wing,claw)

我是大雁类!我有以下特征:
喙的基部较高,长度和头部的长度几乎相等
翅膀长而尖
爪子是蹼状的

  • 类属性:定义在类中,并且在函数体外的属性。类属性可以在类的所有实例之间共享值,也就是在所有实例化的对象中共有
    class Geese:
    ‘’‘大雁’’’
    neck=“脖子较长”
    wing=“振翅频率高”
    leg=“腿位于身份的中心点,行走自如”
    def functionName(self):
    print("我是大雁类!我有以下特征: ")
    print(self.neck)#不能print(neck)这样访问
    print(Geese.wing)
    print(self.leg)
    wildGoose=Geese()
    wildGoose.functionName()

我是大雁类!我有以下特征:
脖子较长
振翅频率高
腿位于身份的中心点,行走自如

  • 实例属性:指定义在类的方法中的属性,只作用于当前实例中
    class Geese:
    ‘’‘大雁’’’
    def functionName(self):
    print("我是大雁类!我有以下特征: ")
    Geese.neck = “脖子较长”
    self.wing = “振翅频率高”
    self.leg = “腿位于身份的中心点,行走自如”
    print(Geese.neck)#不能print(neck)这样访问
    print(self.wing)#不能print(Geese.wing)
    print(self.leg)
    wildGoose=Geese()
    wildGoose.functionName()

我是大雁类!我有以下特征:
脖子较长
振翅频率高
腿位于身份的中心点,行走自如

-对于实例属性也可以通过实例名称修改,与类属性不同,通过实例名称修改实例属性后,并不影响该类的其他实例中相应的实例属性的值。
class Geese:
‘’‘大雁’’’
def init(self):
print("我是大雁类!我有以下特征: ")
Geese.neck = “脖子较长”
self.wing = “振翅频率高”
print(Geese.neck)#不能print(neck)这样访问
print(self.wing)#不能print(Geese.wing)
Goose1=Geese()
Goose2=Geese()
Goose1.neck=“脖子没有天鹅的长”
Goose2.wing=“翅膀很有力”
print(“Goose1的属性:”,Goose1.neck+’ ‘+Goose1.wing)
print(“Goose2的属性:”,Goose2.neck+’ '+Goose2.wing)

我是大雁类!我有以下特征:
脖子较长
振翅频率高
我是大雁类!我有以下特征:
脖子较长
振翅频率高
Goose1的属性: 脖子没有天鹅的长 振翅频率高
Goose2的属性: 脖子较长 翅膀很有力

  1. 访问权限
    foo :首尾双下划线表示定义特殊方法,一般是系统定义名字,如:init()
    _foo :以单下划线开头的表示protected(保护)类型的成员,只允许类本身和子类进行访问,但不能使用“from module import *”语句导入。
    class Swan:
    ‘’‘天鹅类’’’
    _neck_swan=‘天鹅的脖子很长’
    def init(self):
    print(“init():”,Swan._neck_swan)
    swan=Swan()
    print(“直接访问:”,swan._neck_swan)

init(): 天鹅的脖子很长
直接访问: 天鹅的脖子很长

__foo :双下划表示private(私有)类型的成员,只允许定义该方法的类本身进行访问,而且也不能通过类的实例进行访问,但是可以通过“类的实例名._类名__xxx”方式访问。
class Swan:
‘’‘天鹅类’’’
__neck_swan=‘天鹅的脖子很长’
def init(self):
print(“init():”,Swan.__neck_swan)
swan=Swan()
print(“加入类名:”,swan._Swan__neck_swan)
print(“直接访问:”,swan.__neck_swan)

Traceback (most recent call last):
init(): 天鹅的脖子很长
File “C:/Users/mi/Desktop/python/pythonProject/hello world.py”, line 8, in
加入类名: 天鹅的脖子很长
print(“直接访问:”,swan.__neck_swan)
AttributeError: ‘Swan’ object has no attribute ‘__neck_swan’

  1. 类一种特殊的属性
    在Python中,可以通过@property(装饰器)将一个方法转换为属性,从而实现用于计算的属性。将方法转换为属性后,可以直接通过方法名来访问方法,而不需要再添加一对小括号“()”,这样可以让代码更加简洁。如果想要创建一个可以读取,但不能修改的属性,那么可以使用@property实现只读属性。
    class TVshow:
    def init(self,show):
    self.__show=show#私有的实例属性
    @property
    def show(self):
    return self.__show
    tvshow=TVshow(“正在播放新闻联播”)
    print(“默认:”,tvshow.show)

tvshow.show=“正在播放西游记”
print(“修改后:”,tvshow.show)#不能修改,抛出异常

默认: 正在播放新闻联播
Traceback (most recent call last):
File “C:/Users/mi/Desktop/python/pythonProject/hello world.py”, line 10, in
tvshow.show=“正在播放西游记”
AttributeError: can’t set attribute

  1. 继承
    class ClassName(baseclasslist):
    ‘’‘类的帮助信息’’’
    statement

class Fruit:
def init(self,color=“绿色”):
Fruit.color=color
def harvest(self):
print(“水果原来是:”+Fruit.color+“的!”)
class Apple(Fruit):
def init(self):
super().init()#派生类不会自动调用父类的__init__()函数
print(“我是苹果”)
apple=Apple()
apple.harvest()

我是苹果
水果原来是:绿色的!

猜你喜欢

转载自blog.csdn.net/BooleanWater/article/details/88199988