python类定义
在我的收藏中有一篇特别详细的类讲解
此处部分内容引自:http://blog.sina.com.cn/s/blog_59b6af690101bfem.html
class myclass:
'this is my first class of python'
# foo是类属性,相当于static foo是静态成员,可以用类名直接访问
foo=100
# myfun 是类方法,必须由类的实例来调用
def myfun (self):
print
myclass.foo
C=myclass()
C.myfun()
类的特殊属性
myclass 是类定义
print myclass.__name__ output:myclass貌似只有类定义有这个属性,类实例没有这个属性
print myclass.__doc__ output:'this
is my first class of python' 类的文档字符串
print myclass.__dict__ output:类的所有属性和方法,只有类定义有,实例这个属性输出空
print myclass.__module__ output:__main__类定义所在的模块
C是类的实例
print C.__doc__ output:'this is my first class of python' 类的文档字符串,实例也有此属性
print C.__dict__ output:{} 实例没有这个属性,输出为空
print C.__module__ output:__main__ 类定义所在的模块
print C.__class__ output: myclass 实例对应的类名,仅实例有此属性
类的构造
class myclass:
'this is my first class of python'
foo=100
def myfun (self):
print "class's func "
def __init__(self,msg='hello'):
self.msglist=msg //实例属性可以动态的添加,此时是在构造时候添加完成
print 'init'
print myclass.foo
C=myclass()
C.myfun()
print C.msglist
注意,python可以灵活的随时为类或是其实例添加类成员,有点变态,而且实例自身添加的成员,与类定义无关:
//添加一个类实例的成员
C.name='genghao'
现在实例C有了数据成员 name
现在加入这两句
print C.__dict__
print myclass.__dict__
可以看到类定义里面并没有添加成员name,说明它仅仅属于类的实例C
类继承:
class subclass(myclass):
member='sdkfjq'
def func(self):
print "sdfa"
多重继承:
class
multiple_inheritance(myclass,subclass,ortherclass):
def funy():
do what you want to do
测试代码:
class ttt:
name= 42
def __init__(self,voice='hello'):
self.voice=voice#new member for class
def member(self):
self.name=63
self.strane='st' #new member for class
def say(self):
print self.voice
t= ttt()
t.say()
print t.name
t.member()
t.fuc='sdfa'#new member for instance of the class ttt
print t.name
print ttt.__name__
print ttt.__dict__
print t.__dict__
print t.fuc
python类定义的更多相关文章
- python类定义与c#的一些区别
c#中可以定义一个空类,但是python中定义空类需要加pass class EmptyClass(object): pass python的lei是多继承 python子类继承了基类,如果子类也 ...
- python类定义的讲解
python是怎么定义类的,看了下面的文章大家就会了,不用多说,开始学习. 一.类定义: 复制代码代码如下: class <类名>: <语句> 类实例化后,可以使用其属性,实际 ...
- Python类定义和类继承详解
类实例化后,可以使用其属性,实际上,创建一个实例之后,可以通过类名访问其属性,如果直接使用类名修改其属性,那么直接影响已经实例化的对象. 类的私有属性: __private_attrs 两个下划线开头 ...
- python 类定义 继承
0 前言 系统:win7 64bit IDE : python(x,y) 2.7.6.1 IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) ...
- Python类定义、属性、初始化和析构
类的定义,属性和方法概念 方法中self的作用和意义 初始化( __init__ ) 和 析构方法( __del__ ) 类的定义 class 类名 实例 实例名 = 类名() 一个“实例”的“特征” ...
- Python类的探讨
我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法 Python类定义以关键字class开头,一个类定义例 ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- python类的定义和使用
python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...
- Python类的定义
Python笔记--类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
随机推荐
- 如何上传本地项目到gitHub解决方案
最近有人有人问到我怎么将新创建的本地代码上传到github上,这里简单的记录一下,我喜欢使用命令行,这里全用命令行来实现,不了解Git命令的可以去了解下. 1. 建立本地仓库,cd到你想要上传文件的 ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 3.2 2-dim Vector Initialization
声明3行4列的数组 const int m = 3, n = 4; vector<vector<int> > A(m); // 3 rows for(int i = 0; i ...
- CentOS 7下GitLab搭建及配置
由于公司业务,需要上Git版本控制. * 目前市面上比较有名的Git服务提供商,国外有GitHub.BitBucket.GitLab,国内有码云,Coding. * 现有的服务商,对于免费的套餐都有一 ...
- MAX(字段)加0与不加0的测试
--max(字段名)中的"字段名"的数据类型是字符型的,"字段名"+ 0后,oracle会隐式的转换成数字型 --测试 )); insert into Test ...
- ACM | HDU|6227_Rabbit
题意: 有n只兔子分别占据不同的位置,任意一只兔子可以插入任意两只兔子的之间,但要求两只兔子之间要有空位,求这样的移动次数最多能够有多少? 在这里每一只兔子没有区别,可以看做把 ...
- Python3 CGI编程
什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...
- POSIX 消息队列相关问题
一.查看和删除消息队列要想看到创建的posix消息队列,需要在root用户下执行以下操作:# mkdir /dev/mqueue# mount -t mqueue none /dev/mqueue删除 ...
- Python强大的可变参数传递机制
今天模拟定义map函数.写着写着就发现Python可变长度参数的机制真是灵活而强大. 假设有一个元组t,包含n个成员: t=(arg1,...,argn) 而一个函数f恰好能接受n个参数: f(arg ...