python学习笔记-Day7
class Province: # 静态字段(类变量/属性)
country = '中国' def __init__(self, name): # 普通字段(实例变量/属性)
self.name = name # 直接访问普通字段
obj = Province('河北省')
print obj.name # 直接访问静态字段
Province.country
复制代码
class Animal:
def __init__(self, name): # Constructor of the class
self.name = name
self.__num = None #私有化变量,不允许外部访问
hobbie = 'meat' @classmethod #类方法,不能访问实例变量
def talk(self):
print('is hobbie %s'%self.hobbie) @staticmethod #静态方法,不能访问类变量和实例变量
def walk(self):
print('is walk %s'%self.hobbie) @property #把方法变为属性
def habit(self):
print("%s habit is " %self.name)
@property
def total_player(self):
return self.__num @total_player.setter # 修改属性
def total_player(self,num):
self.__num = num
print("total num:%s"%self.__num) @total_player.deleter # 删除
def total_player(self):
print("total player got deleted")
del self.__num d = Animal("anonymous")
print(d.total_player)
d.total_player = 3 #直接穿变量
d.__num = 9
print("Out:",d.__num)
print("to access private variable:",d._Animal__num)
# del d.total_player
print("Inside:",d.total_player)
#广度优先 先从左向右找同级别,再找上一级 新式类 class name(object)
#深度优先,先找左面第一个,如果没有,找上级然后在返回同级找。旧式类 class name class A(object):
n = 'A'
def f1(self): print("from A") class B(A):
n = 'B'
def f1(self):
print('from B') class C(A):
n = 'C'
def f1(self):
print("from C")
class D(B,C):
pass
p = D()
p.f1()
python学习笔记-Day7的更多相关文章
- Python学习笔记 - day7 - 类
类 面向对象最重要的概念就是类(Class)和实例(Instance),比如球类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同.在Python中,定义类 ...
- 【目录】Python学习笔记
目录:Python学习笔记 目标:坚持每天学习,每周一篇博文 1. Python学习笔记 - day1 - 概述及安装 2.Python学习笔记 - day2 - PyCharm的基本使用 3.Pyt ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- python学习笔记--Django入门四 管理站点--二
接上一节 python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
随机推荐
- js监听rem实现响应式
原文链接:http://caibaojian.com/web-app-rem.html (function (doc, win) { var docEl = doc.documentElement, ...
- pt-fifo-split使用
percona-toolkit系列-pt-find http://blog.itpub.net/23249684/viewspace-1354308/ 在<mysql插入/更新数据>这篇文 ...
- U盘安装Win7操作系统
玩转Windows7系统镜像四部曲 Step 1: 下载Win7 ISO系统镜像 温馨提示:请您尽量选用Win7之家提供的官方原版镜像安装,因为正版比各种所谓的"精简版.纯净版" ...
- Hello Dojo!(翻译)
http://dojotoolkit.org/documentation/tutorials/1.10/hello_dojo/index.html 欢迎学习DOJO!在本教程中,你将学些到如何加载DO ...
- [zz]Lessons from Pixar: Why Software Developers Should Be Storytellers
http://firstround.com/article/lessons-from-pixar-why-software-developers-should-be-story-tellers Whe ...
- Dictionary<TKey, TValue> 类
C# Dictionary<TKey, TValue> 类 Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及 ...
- mysq修改密码
1.确定你的mysql 是否能正常工作登录数据库cmd--"命令提示字符"窗口录入,录入cd C:\mysql\bin 并按下回车键,将目录切换为 cd C:\mysql\bin再 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- Jmeter安装
安装下载方法:http://www.jb51.net/softjc/266834.html 下载地址:http://jmeter.apache.org/download_jmeter.cgi Wind ...
- 创建线程方式-NSOperation
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...