python笔记之类
类
python不直接支持私有方式,可以在方法或者属性之前加上双下划线,将其变为私有,即外部无法直接调用
访问私有方法或者属性,方法是: _<类名><变量名>
首先类定义
# -*- coding: UTF-8 -*-
#!/usr/bin/python
#__metaclass__ = type
class Person:
#self是对象自身的引用,将name值放在自己的命名空间中
def setName(self, name):
self.name = name
def getName(self):
print self.name
return self.name
class NewPerson(object):
def setNewName(self, name):
self.__name = name
def __getName(self):
print self.__name
return self.__name
class Student(Person):
def sayHello(self):
print "hello, my name is " + self.name
#多继承
class LazyBoy(NewPerson, Student):
pass
练习
shoren = Person() #初始化对象
shoren.setName("shoren's name ... ") #设置
shoren.getName()
print shoren.name #直接调取用户名
#方法也可以当做一个变量,是可以随意绑定的
getMyName = shoren.getName
getMyName()
np = NewPerson();
np.setNewName("new name")
try:
print np.__name #不能直接访问私有变量
except Exception, e:
print e
try:
print np.__getName #不能直接调用私有方法
except Exception, e:
print e
print np._NewPerson__name #使用 _<类名><变量名> 访问私有变量
np._NewPerson__getName()
stu = Student();
stu.setName("踏岚")
stu.sayHello()
lb = LazyBoy();
lb.name = "lazy boy"
lb.sayHello()
结果:
shoren's name ...
shoren's name ...
shoren's name ...
'NewPerson' object has no attribute '__name'
'NewPerson' object has no attribute '__getName'
new name
new name
hello, my name is 踏岚
hello, my name is lazy boy
可用的方法:
- isinstance(object, class) 对象是否是类的实例
- issubclass(A, B) A是否为B的子类
- hasattr(object, name) 对象是否有给定的属性
- getattr(object, name[, default]) 获取属性的值,可以提供默认值
- setattr(object, name, value) 设置对象属性值
- type(object) 返回对象类型 [使用__metaclass__=type 或从object继承的类,可以使用此方法查看类型]
#相关函数练习
print isinstance(stu, Student)
print issubclass(Student, Person)
print hasattr(stu, "name")
print hasattr(stu, "getName")
print dir(np)
print NewPerson.__base__
print dir(stu)
print stu.__class__
print type(stu)
print type(np)
结果:
True
True
True
True
['_NewPerson__getName', '_NewPerson__name', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'setNewName']
<type 'object'>
['__doc__', '__module__', 'getName', 'name', 'sayHello', 'setName']
__main__.Student
<type 'instance'>
<class '__main__.NewPerson'>
由上,NewPerson继承至object,所以含有更多的属性,且使用type()得到正确的值。如果在文件中加入__metaclass__ = type,则stu.class 和type(stu)值均为 <class 'main.Student'>
python笔记之类的更多相关文章
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7-1 之面向对象编程
python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
- python笔记 - day7
python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...
- python笔记 - day6
python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...
随机推荐
- python中__name__=='__main__'的作用
学习python语法的过程中碰到了__name__=='__main__',这里做个笔记. 作用 这段代码的作用就是让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 测试 先 ...
- strcpy、strncpy与memcpy的区别与使用方法
strcpy.strncpy.memcpy这三个C语言函数我们在主机代码编写中会很频繁的使用到,但是三个函数的区别.使用时该注意什么还是有必要说下的. 本文参考<C 标准库>编写. 一.函 ...
- 6.2 PowerPC处理器如何处理MSI中断请求
PowerPC处理器使用OpenPIC中断控制器或者MPIC中断控制器,处理外部中断请求.其中MPIC中断控制器基于OpenPIC中断控制器,但是作出了许多增强,目前Freescale新推出的Powe ...
- R︱Linux+Rstudio Server尝鲜笔记(打造最佳Rstudio体验+报错的解决方案)
Rstudio Server 是Rstudio开发的基于R语言的网页版(只能在Linux),你在手机上都可以运行R,还是挺方便的.就是配置起来有点麻烦. 官方下载链接:https://www ...
- RTLinux编程总结
做过一个有关RTLinux的项目,时间一长,差不多忘光了,现在尽量把原来做过的东西总结一下,以备后用,同时正在做类似项目的一个借鉴平台主机:redhat 8.0目标机:PC104模块.ISA总线脉冲输 ...
- VS2005 添加onTimer定时器
SetTimer(1,300,NULL); void CchangeDisplayDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调 ...
- [linux]device eth0 does not seem to be present, delaying initialization
mlite虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟硬盘使用之前的,启动系统后不能上网,通过ifconfig查看网卡没启动,遂启动网卡服务,但是出错,就是:device eth0 does ...
- Python 文件的输入与输出
1. 文本文件的读写主要通过open()所构建的文件对象来实现.我们打开一个文件,并使用一个对象来表示该文件 , f = open(d,r) 其中d是文件名,r是模式 "r" 文件 ...
- Linux开机启动图片修改
Linux启动时会在屏幕上显示一个默认的开机图片,我们可以修改成为自己的图片,需要做以下工作 软件gimp下载地址:http://www.rayfile.com/zh-cn/files/0bb556b ...
- VxWorks启动过程详解(上)
vxworks有三种映像: VxWorks Image的文件类型有三种 Loadable Images:由Boot-ROM引导通过网口或串口下载到RAM ROM-based Images(压缩/没有压 ...