码迷,mamicode.com
首页 > 其他好文 > 详细

备忘录v0.27

时间:2018-05-24 23:05:04      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:python

备忘录v0.27
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.5 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d.\d‘)

re_date.findall(mem_text)
[‘1.1‘, ‘1.2‘, ‘135‘, ‘222‘, ‘232‘, ‘1.3‘, ‘1.4‘, ‘1.5‘]
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.5 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d\.\d‘)  # . 表示一个任意字符,需要转义 
re_date.findall(mem_text)
[‘1.1‘, ‘1.2‘, ‘1.3‘, ‘1.4‘, ‘1.5‘]
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.5 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d.\d‘)
re_date = re.compile(r‘\d\.\d‘)
re_date = re.compile(r‘\d+\.\d+‘)  # + 表示匹配前面的字符不止一次

re_date.findall(mem_text)
[‘1.1‘, ‘1.2‘, ‘1.3‘, ‘1.4‘, ‘1.5‘]
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.5 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d.\d‘)
re_date = re.compile(r‘\d\.\d‘)
re_date = re.compile(r‘\d+\.\d+‘)  # + 表示匹配前面的字符不知一次
re_date = re.compile(r‘(\d+)\.(\d+)‘)  # 分组然后sub替换

# re_date.findall(mem_text)
re_date.sub(r‘\1月\2日‘, mem_text)
‘\n\n1月1日 安排coop编写一个脚本\n1月2日 记录一下安总的电话 13522223232\n1月3日 修改python程序的bug\n1月4日 回家路上买些水果,蔬菜\n1月5日 事情还有很多,需要重新安排下\n‘
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.15 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d.\d‘)
re_date = re.compile(r‘\d\.\d‘)
re_date = re.compile(r‘\d+\.\d+‘)  # + 表示匹配前面的字符不知一次
re_date = re.compile(r‘(\d+)\.(\d+)‘)  # 分组然后sub替换

# re_date.findall(mem_text)
print(re_date.sub(r‘\1月\2日‘, mem_text))  # print 处理\n ,回车符
1月1日 安排coop编写一个脚本
1月2日 记录一下安总的电话 13522223232
1月3日 修改python程序的bug
1月4日 回家路上买些水果,蔬菜
1月15日 事情还有很多,需要重新安排下
import  re
mem_text = """

1.1 安排coop编写一个脚本
1.2 记录一下安总的电话 13522223232
1.3 修改python程序的bug
1.4 回家路上买些水果,蔬菜
1.5 事情还有很多,需要重新安排下
"""
re_date = re.compile(r‘\d.\d‘)
re_date = re.compile(r‘\d\.\d‘)
re_date = re.compile(r‘\d+\.\d+‘)  # + 表示匹配前面的字符不知一次
re_date = re.compile(r‘(\d+)\.(\d+)‘)  # 分组然后sub替换
re_date = re.compile(r‘(?P<month>\d+)\.(?P<day>\d+)‘)
# 变量month,day表达的更清晰  ?P<变量> 固定格式
# \g<变量>  固定格式

re_date.findall(mem_text)
# print(re_date.sub(r‘\1月\2日‘, mem_text))  # print 处理\n ,回车符
print(re_date.sub(r‘\g<month>月\g<day>日‘,mem_text))
1月1日 安排coop编写一个脚本
1月2日 记录一下安总的电话 13522223232
1月3日 修改python程序的bug
1月4日 回家路上买些水果,蔬菜
1月5日 事情还有很多,需要重新安排下

class People:
    """定义一个人类"""
    def __init__(self, name, age):  # 初始化方法,头尾双下划线表示特殊方法
        self.name = name        # 类的属性,也就是特点,,特征
        self.age = age
    def walk(self):            # 不同方法,函数、method、function
        "人类的第一步"
        print(f‘{self.name} is walking‘)  # 表示动作,行动,能做的事儿
class Car:
    pass
Car
__main__.Car
c = Car()  # 实例  注意括号
c
<__main__.Car at 0x1cf9b0472e8>
isinstance(c, Car)  # 查看c是否是某个类的实例
True

私有变量: __name,不能被继承

内部变量: _开头

通过方法修改私有数据,对数据进行保护

class  Car:
    name = ‘coop‘
    def __init__(self, brand, price, wheels, power):
        self._brand = brand  # brand内部变量
        self.price = price 
        self.wheels = wheels
        self.power = power
        self.__speed = 0 # 私有变量
    def run(self,action):
        print(f‘{self.brand} is running‘)
        if action == ‘1‘:
            self.__speed += 1*10  # 修改私有变量
            print(f‘当前速度是:{self.__speed}‘)
    def start(self):
        print(f‘{self.brand} is on‘)

    @property  # 装饰器,将speed函数转换成类似与属性的概念,目的:
    def speed(self):  # 只读,getter方法,获取原始值,不能修改
        return self.__speed

    @property
    def brand(self):
        return self._brand

    @brand.setter  # 添加setter方法,可以被赋值
    def brand(self,brand):
        if not isinstance(brand,str):
            raise TypeError(‘牌子是字符串类型‘)  #抛出异常 raise
        self._brand = brand  # 可以对属性进行操作,提前判断

    @property
    def info(self):
        return f‘{self.brand}:{self.price}‘
dazong = Car(‘dazong‘,10,4,‘oil‘)  # 必须现有这一步骤么?
dazong.speed
0
dazong.brand
‘dazong‘
dazong.info
‘dazong:10‘
dazong.price
10
dazong.wheels
4
dazong.start
<bound method Car.start of <__main__.Car object at 0x00000297C1D99160>>
dazong.run(‘2‘)  # 只有这run()和start()使用了括号()
dazong is running
dazong.start()
dazong is on
dazong.power
‘oil‘
dazong
<__main__.Car at 0x202af11ac50>
dazong.run(1)
dazong is running
dazong.run(‘1‘)
dazong is running
当前速度是:10
###################
dazong.info   #区别于dazong.run(),是因为有了@property的原因
‘dazong:10‘
dazong.speed
10
################
dazong.brand
‘dazong‘
dazong.brand = ‘dazong2018‘  # 对属性brand进行了修改
dazong.brand
‘dazong2018‘
dazong.brand = 1111  # 函数中加了判断语句isinstance  
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-26-5d2373d32d4a> in <module>()
----> 1 dazong.brand = 1111

<ipython-input-12-bfe1a20f9dc0> in brand(self, brand)
     26     def brand(self,brand):
     27         if not isinstance(brand,str):
---> 28             raise TypeError(‘牌子是字符串类型‘)  #抛出异常 raise
     29         self._brand = brand  # 可以对属性进行操作,提前判断
     30 

TypeError: 牌子是字符串类型
Car.brand
<property at 0x1f12c82df98>
tesla = Car(‘tesla‘,10000,4,‘electric‘)
tesla.run(‘1‘)
tesla is running
当前速度是:10
tesla.price
10000
tesla.price = 100
tesla.price
100
###################
class  Car:
    name = ‘coop‘
    def __init__(self, brand, price, wheels, power):
        self._brand = brand  # brand内部变量
        self.price = price 
        self.wheels = wheels
        self.power = power
        self.__speed = 0 # 私有变量
    def run(self,action):
        print(f‘{self.brand} is running‘)
        if action == ‘1‘:
            self.__speed += 1*10  # 修改私有变量
            print(f‘当前速度是:{self.__speed}‘)
    def start(self):
        print(f‘{self.brand} is on‘)

    @property  # 装饰器,将speed函数转换成类似与属性的概念,目的:
    def speed(self):  # 只读,getter方法,获取原始值,不能修改
        return self.__speed

    @property
    def brand(self):
        return self._brand

    @brand.setter  # 添加setter方法,可以被赋值
    def brand(self,brand):
        if not isinstance(brand,str):
            raise TypeError(‘牌子是字符串类型‘)  #抛出异常 raise
        self._brand = brand  # 可以对属性进行操作,提前判断

    @property
    def info(self):
        return f‘{self.brand}:{self.price}‘
# 继承  super()继承父类
class Tesla(Car):
    def __init__(self, brand=‘Tesla‘, price=100,wheels=4,power=‘electric‘):
#        self._brand = brand  # brand内部变量
#        self.price = price 
#        self.wheels = wheels
#        self.power = power
#        self.__speed = 0 # 私有变量
        super().__init__(brand, price, wheels, power)  # 调用父类,继承的就是上面注释掉的属性

    def run(self, action):
        print(f‘{self.brand} is running with {self.power}‘)
t = Tesla()
t.price
100
t.price = 99
t.price
99
isinstance(t,Tesla)
True
isinstance(t,Car)
True
# 多态,各个子类都有自己的方法,当没有的时候调用父类的方法,这种状况成为多态
t.run(‘1‘)
Tesla is running with electric
c = Car(‘car‘, 200, 5, ‘oil‘)  # 调用的是父类得方法
c.run(‘1‘)
car is running
当前速度是:10
####################

元编程

备忘录v0.27

标签:python

原文地址:http://blog.51cto.com/13118411/2120062

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!