新手上路请多担待

1

2

封装

3

私有化封装

#__author : 'liuyang'
#date : 2019/3/29 0029 上午 9:35
# 不想让别人看 修改 我的属性
# 源码来说
# 用户名 和 密码
# 狭义上的封装 : 私有化的方法 和属性
# 广义上的封装 : 把方法和属性根据类别装到类中 #方法\静态变量\实例变量(对象属性)都可以私有化
#所谓的私有化:
#就是只能在类的内部课件,
#类的外部是不能访问或者查看
class Goods:
def __init__(self,name,proce):
self.name = name
self.__price = proce#私有属性
def get_price(self):
print(self.__price)
apple = Goods('苹果',2)
# print(apple.get_price().__price)
apple.get_price()
import hashlib
# class Auto:
# def __init__(self,name,pwd):
# self.name = name
# self.pwd = name
# def md5(self):
# md5 =
# md5.update() class Goods:
def __init__(self,name,price):
self.name = name
self.__price = price
def get_price(self): #内部 self._类名__属性
print(self.__price)
def set_num(self):
self.__num = 20
apple = Goods('苹果',5)
# print(apple.__price) #AttributeError: 'Goods' object has no attribute '__price'
print(apple.__dict__) #{'name': '苹果', '_Goods__price': 5}
print(apple._Goods__price) #5 #私有的形成
# 定义了私有的只不过 是改了一个名 并不是真正的隐藏了 隐姓埋名
# 所有的私有的变化都是 在类的[内部]定义完成的
apple.__num = 10
print(apple.__dict__)
# print(self._apple__selfnum)
class Foo:
def __init__(self):
self.__func()
def __func(self): #私有化 _Foo__func()
print('foo')
class Son(Foo): #_Son__func() 指针去找init init 找_Foo__func()
def __func(self):
print('son')
Son() #改名了 class User:
def __wahaha(self):
print('') ##self._User__wahaha()
class V(User): #AttributeError: 'V' object has no attribute '_V__wahaha'
def func(self): #self._V__wahaha()
self.__wahaha()
# V().func()
#私有的这个概念: 单反不在一个类里就报错
#私有的所有内容: 实例变量(对象属性)
#静态变量(类变量),方法都不能被子类继承 class User:
def wahaha(self):
print('') ##self._User__wahaha()
class V(User): #AttributeError: 'V' object has no attribute '_V__wahaha'
def func(self): #self._V__wahaha()
self.wahaha()
V().func() #1 func()执行了 类().方法()
# 不是 __init__ 的 V() # 公有的 再累的内部随便用public
# 其它语言里 外部不能用 可以继承的 保护的
#私有的 private 只能在类的内部使用 既不能被继承 也不能再累的外部使用

4

property

# 私有 property 是一对好搭档
# 圆形类
# 计算面积 计算周长
# 半径 面积 周长 --->属性 class Circle:
def __init__(self,r):
self.r = r
@property #把装饰的一个方法伪装成一个属性
def area(self):
return 3.14*self.r**2
c1 = Circle(5)
print(c1.r)
c1.r = 10
print(c1.area) #更符合属性
import time
class Person:
def __init__(self,name,birth):
self.name = name
self.birth = birth
@property
def age(self):
struct = time.localtime()
age = struct.tm_year - int(self.birth.split('-')[0])
return age liuda = Person('liuda','1999-8-19') print(liuda.age)

私有概念+property 不能改

#__author : 'liuyang'
#date : 2019/3/29 0029 上午 11:08
#一个属性 只让看不让改
class Goods:
dicount = 0.8 # 全场折扣
print(dicount) #这里可以
def __init__(self,name,price):
self.name= name
self.__price = price
@property #只支持 obj.price 的方式查看这个结果
def price(self): #这个编程了self.price
# 在里面用得用self.dicount
return self.__price * self.dicount #下一个是静态变量
#self.dicount 和 Goods.dicount 的区别是 类名改了就得又改 #原价格 @price.setter # 1,前者伪装过 2.后两个和前面的同名
def price(self,value):
if type(value) is int or type(value) is float:
self.__price = value
# print('--->',value) apple = Goods('shu',5)
apple2 = Goods('shus',2)
print(apple.price) # apple.price = 10 # 只能看不能改
apple.__price = 1000 # 不能改 这里 随便改
print(apple.__price) #
print(apple.price) # Goods.dicount =1
print(apple.price)
print(apple2.price)
# apple.__price = 2
apple.price = 8 #对应的调用的是被setter装饰的price方法
print(apple.price) #对应调用的是被property装饰的price # 后面很多条件
# 如果我们定义的是普通的变量或者属性
#那么这个属性可以从外部直接访问
#可以任意的修改 obj.attr = 123
#甚至可以删除 del obj.attr
#私有化
#把一个属性加上双下划线__属性名
#这个属性就连在外面看都看不见
#我们实际上有些场景允许别人看,不许改
#__属性
#@property 装饰的属性名
#def 属性名():
#我们允许别人看,也允许别人改,但是不能瞎改,有一些要求
# __属性
# @property 装饰的属性名
# def 属性():return__属性 # @属性.setter
# def 属性(self ,value):
#加点条件
#
#__author : 'liuyang'
#date : 2019/3/29 0029 上午 11:08
#一个属性 只让看不让改
class Goods:
dicount = 0.8 # 全场折扣
print(dicount) #这里可以
def __init__(self,name,price):
self.name= name
self.__price = price
@property #只支持 obj.price 的方式查看这个结果
def price(self): #这个编程了self.price
# 在里面用得用self.dicount
return self.__price * self.dicount #下一个是静态变量
#self.dicount 和 Goods.dicount 的区别是 类名改了就得又改 #原价格 @price.setter # 1,前者伪装过 2.后两个和前面的同名
def price(self,value):
if type(value) is int or type(value) is float:
self.__price = value
# print('--->',value) @price.deleter
def price(self):
del self.__price # 有这个才删除
print('执行我了') apple = Goods('shu',5)
apple2 = Goods('shus',2)
print(apple.price) # apple.price = 10 # 只能看不能改
apple.__price = 1000 # 不能改 这里 随便改
print(apple.__price) #
print(apple.price) # Goods.dicount =1
print(apple.price)
print(apple2.price)
# apple.__price = 2
apple.price = 8 #对应的调用的是被setter装饰的price方法
print(apple.price) #对应调用的是被property装饰的price ## apple.__price = 2 不能 所以 #@price.setter
# del apple.__price 不能外部删除 所以#@price.deleter apple.price #@property
apple.price = 9 #@price.setter #函数里得有修改
del apple.price #@price.deleter #函数里得有删除
print(apple.__dict__)
#apple.__price = 1000 # 不能改 这里 随便改
#{'name': 'shu', '__price': 1000} # 后面很多条件
# 如果我们定义的是普通的变量或者属性
#那么这个属性可以从外部直接访问
#可以任意的修改 obj.attr = 123
#甚至可以删除 del obj.attr
#私有化
#把一个属性加上双下划线__属性名
#这个属性就连在外面看都看不见
#我们实际上有些场景允许别人看,不许改
#__属性
#@property 装饰的属性名
#def 属性名():
#我们允许别人看,也允许别人改,但是不能瞎改,有一些要求
# __属性
# @property 装饰的属性名
# def 属性():return__属性 # @属性.setter
# def 属性(self ,value):
#加点条件
#修改__属性 #私有的:通过给__名字这样的属性 或者方法加上当前所在类的前缀,把属性隐藏其阿里
#只能在本类的内部使用,不能再类的外部使用,不能被继承 #property 把一个方法 伪装成属性 #property 和私有的两个概念一起用
#定义一个私有的
#在定义一个同名共有的方法, @property装饰 #在定义一个同名共有的方法,@方法名.setter
#在定义一个同名共有的方法,@方法名.deleter

6  类和self   classmethod

#__author : 'liuyang'
#date : 2019/3/29 0029 下午 12:22
class Goods:
dicount = 0.8 # 全场折扣
print(dicount) #这里可以
def __init__(self,name,price):
print(self,'')
self.name= name
self.__price = price
@property #只支持 obj.price 的方式查看这个结果
def price(self): #这个编程了self.price
# 在里面用得用self.dicount
return self.__price * self.dicount #下一个是静态变量
#self.dicount 和 Goods.dicount 的区别是 类名改了就得又改
# self.dicount 不好使 ,goods.dicount keyi
# def chang_disount(self,value): #self没有用
# Goods.__discount = value # 用类
@classmethod #把一个对象方法改成了类方法 #把self改成了类
def chang_disount(cls,value): #self没有用
print(cls,'',Goods) #<class '__main__.Goods'> 1 <class '__main__.Goods'>
cls.__discount = value # 用类 @classmethod
def get_discount(cls):
return cls.__discount
# Goods.chang_disount() #
A = Goods('apple',2) Goods.chang_disount(12)
A.chang_disount(12)
print(A.get_discount()) # 类方法
#1.有些时候我们要修改的是类中的静态变量/类变量
# 此时根本不会和self有任何的操作关联
#这时传一个 self参数对我们完全没有用
#我们希望接受的是当前我们所在的类
appp = Goods('apple',2)
appp.chang_disount(1)
print(Goods.get_discount()) # 类方法 推荐使用类名调用而不是使用对象名调用
# 没写死 自动传 类名

7  自己想代码 想python语法的用法  想龟叔的想法 python 设计思想

class A:
# def func(): #不错
@staticmethod #声明一个普通的不会使用类相关的方法
def func(): #此时func是一个静态方法
print('既不操作和self相关')
print('也不操作和类名相关')
A.func() # login() 登录功能
class Student:
def __init__(self,name,age):
self.name = name
self.age =age
@staticmethod
def login(self):pass #先获取这个学生的用户名和密码
#判断他登录成功之后进行实例化 # a = Student('liuda',2)
Student.login()
stu = Student('liuda') # 先进行思考
#方法就在那,
#自己有想法,先学,去想python的用法
# 有一些也可能是老师也没想到的

#  思维导图写

#凌晨5点备课到 8点上课  讲课到凌晨1点   下课凌晨2点 上海的分培训机构

python学习 day22 (3月29日)----(生成器推导式)的更多相关文章

  1. python学习 day11 (3月16日)----(生成器内置函数)

    1生成器 1生成器的本质 一定是迭代器(反之不一定(用send(生成器特有方法)验证))2生成器是可以让程序员自己定义的一个迭代器3生成器的好处,节省内存空间4生成器的特性,一次性的,惰性机制,从上往 ...

  2. Python 学习日志9月20日

    9月20日 周三 多大年龄了,还活得像个小孩.——急什么,人生又不长. 你习惯了思考宇宙星辰,一百年真的不长,一生也就不那么长,许多人的价值观念你也就无法理解.同样,许多人也无法理解你的价值观念,感兴 ...

  3. Python 学习日志9月19日

    9月19日 周二 今天是普通的一天,昨天也是普通的一天,刚才我差点忘记写日志,突然想起来有个事情没做,回来写. 今天早晨学习<Head First HTML and CSS>第十一章节“布 ...

  4. Python学习日志9月17日 一周总结

    周一,9月11日 这天写的是过去一周的周总结,我从中找出当天的内容. 这天早晨给电脑折腾装机,早晨基本上没有学习,休息了一个早晨. 下午写的上周总结,完事做mooc爬虫课的作业,<Think P ...

  5. Python 学习日志9月21日

    9月21日 周四 今天是个特殊的日子吗,总感觉9月21这个日子听着怪怪的. 今天早晨看<Head First HTML and CSS>第13章节“表格和更多列表”,内容不多,看完并做了详 ...

  6. Python学习日志9月13日

    昨天的学习日志没有写,乱忙了一整天,政治电脑. 好奇心重,想要给电脑装上传说中LInux操作系统,各种小问题折腾到半夜,今天又折腾到晚上才真正的装上系统. 可是装上系统后又发现各种的不好用.虽然界面比 ...

  7. python学习笔记:第12天 列表推导式和生成器

    目录 1. 迭代器 2. 推导式 1. 迭代器 什么是生成器呢,其实生成器的本质就是迭代器:在python中有3中方式来获取生成器(这里主要介绍前面2种) 通过生成器函数获取 通过各种推导式来实现生成 ...

  8. Python学习日志9月16日

    刚才我差点睡着了,差资料的时候太费神,有些累. 今天早晨学习了<head first HTML and CSS>,今天把昨天没看了的关于字体和颜色的一章节看完了,真长.我详细的做了笔记,并 ...

  9. python学习 day21 (3月28日)----(抽象类 多态 nametuple dump)

    不要因为走的路太久了,而忘记了为了什么而出发. 提前作准备了吗?把思维导图的东西做了吗? 和工作了几年的人,相比,是不是相同的水平,如果要写简历的话. 一边学习,一边复习. 小就是大,少就是多. 1. ...

随机推荐

  1. Maven 标签

    scope 1.compile:默认值 他表示被依赖项目需要参与当前项目的编译,还有后续的测试,运行周期也参与其中,是一个比较强的依赖.打包的时候通常需要包含进去 2.test:依赖项目仅仅参与测试相 ...

  2. python读写文件中read()、readline()和readlines()的用法

    python中有三种读取文件的函数: read() readline() readlines() 然而它们的区别是什么呢,在平时用到时总会遇到,今天总结一下. 0. 前期工作 首先新建一个文件read ...

  3. UVA-10054.The Necklace(欧拉回路)解题报告

    2019-02-09-21:55:23 原题链接 题目描述: 给定一串珠子的颜色对,每颗珠子的两端分别有颜色(用1 - 50 之间的数字表示,对每颗珠子的颜色无特殊要求),若两颗珠子的连接处为同种颜色 ...

  4. Xcode调试与其他

    在项目中设置main接收的参数,模拟终端输入 Product->Scheme->Edit Scheme->Run->Arguments 例: 相当于在终端执行命令:./ac-t ...

  5. MongoDB的索引(六)

    数据准备:在mongodb命令行终端执行如下代码 for(var i=0;i<100000;i++) { ... db.users.insert({username:"user&quo ...

  6. cgi,fast-cgi,php-cgi,php-fpm转载详解

    转载自:https://segmentfault.com/q/1010000000256516 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编 ...

  7. C#实现发送给QQ邮件

    最近在做一个通过点击忘记密码往用户邮箱中发邮件(邮件内容是一个超链接)点击进行修改的功能,发送原理,我们只是把邮件发送给smtp服务器,然后再由smtp服务器发送到邮箱,发送之前要校验一下. 1.微软 ...

  8. 移动端 input 输入框实现自带键盘“搜索“功能并修改X

    主要利用html5的,input[type=search]属性来实现,此时input和type=text外观和功能没啥区别: html代码入下: <form action="" ...

  9. Varnish 入门

    本文将介绍 varnish 的工作流程,安装以及 varnish 的配置三个方面的内容.首先简单的介绍 varnish 以及其工作流程,大概了解其内部原理,然后介绍了 varnish 的安装方法,最后 ...

  10. devexpress WinForms MVVM

    WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...