python 中的内置函数 与 类
来源:互联网 发布:m1a2sep各项数据 编辑:程序博客网 时间:2024/05/22 06:14
python这种语言的变量命名规则有些奇葩,我相信大家已经遇到过了,比如:__future__。这种特殊的形式表明,这个
变量有特殊的意义,你不可以在自己的代码中创造这类变量。
1.__init__() 构造函数
类似于c++,java等面向对象语言,python 在类的定义中也有构造函数,这就是__init__()
__init__(parameters)中的变量parameter用来构建这个类的变量
例如:
class newclass(object):"""docsng for newclass""" def __init__(self, aStr): self.aStr = aStrprint "constructor is been used. "+self.aStr+" has been created"hahaha = newclass("hello")
结果就是,constructor is been used. hello has been created
2.__del__() 析构函数
python 不需要进行内存管理,当一个类被扔到垃圾箱(garbage-collected)的时候,这个函数就会发挥功效。但是,你不会知道具体它什么时候起作用,还是远离它比较好。
3说说__init__()被重载(override)的情况
class Bird(object): def __init__(self): self.hungry = True def eat(self):if self.hungry: print "Great" self.hungry = Falseelse: print "No,thanks"
如果运行如下程序:
aBird = singBird()aBird.eat()程序会报错,因为没有了self.hungry这个变量
解决办法为:
class singBird(Bird): def __init__(self):Bird.__init__(self)print "i am singing"
或
class singBird(Bird): def __init__(self):super(singBird,self).__init__()print "i am singing"
4.简述python类中常用的内置函数
假设我们有一个字典 aDict = {'a': 1,‘b’:2}
__len__(self) 返回序列中收集item的个数 ,对于空的字典,列表,字符串,元组,它们的逻辑值为False
相当于len(aDict)
__getitem__(self,key) 返回与key相对应的值 。相当于aDict[key]
__setitem__(self.key,value) 将与key相对应的值设为value 。相当于aDict[key] = value
__delitem__(self,key) 将key与和key对应的item删掉 del(aDict[key])
对于序列(元组,列表,字符串)aList[-n] 相当于 aList[len(aList)-n]
5.静态方法(staticmethod)和类方法(classmethod)
class myclass(object): def smeth(): print "this is a static method" def cmeth(cls): print "this is a class method",cls cmeth = classmethod(cmeth)
这就是传说中的装饰器
class myclass(object): @staticmethod def smeth(): print "this is a static method" @classmethod def cmeth(cls): print "this is a class method",cls当你使用@字符时,装饰器(deractor)的使用就更加简便。
调用myclass.smeth() 输出 this is a static method
调用myclass.cmeth() 输出 this is a class method <class '__main__.myclass'>
6.attribute 函数
__getattribute__(slef,name) 当使用名字为name的attribute时自动调用
__getattr__(self,name) 当使用名字为name的attribute时,实际上并不存在名字为name的attribute时,自动调用
__setattr__(self,name,value)赋值自动调用
__delattr__(self,name) 删除自动调用
例如:
class Rectangle(object): def __init__(self): self.width = 0 self.heigh = 0 def __setattr__(self,name,value): if name == 'width': self.width =value elif name == 'heigh': self.heigh = value else: raise AttributeError def __getattar__(self,name): if name == 'width': return self.width elif name == 'heigh': return self.heigh else: raise AttributeError
7. 迭代器Iterator
迭代器的功能非常强大,可以对任何对象进行循环
迭代的方式可以利用next()函数,或是for 循环
利用next()函数要注意:最后一个对象后,再进行next(),会产生StopIteration报错
注意:在python 3.0中,next() 函数变为__next__()
class Fibb(object): def __init__(self): self.a = 0 self.b = 1 def next(self): self.a,self.b = self.b, self.a + self.b return self.a def __iter__(self): return selffib = Fibb()print next(fib)print fib.next()for i in fib: if i >800: print i break
总而言之,含有__iter__()的类都是可迭代的,
含有next()的类才是迭代器。
所以对可迭代的类可以使用iter()函数
例如:
a =iter("anasiaj")print a.next()print a.next()于是,有的人就会问,自己创造的迭代器,可不可以自己设定结束点呢?
当然可以,StopIteration 就相当于循环中的break
例子如下:
class aIter(object): def __init__(self): self.value = 0 def next(self): self.value += 1 if self.value > 15: raise StopIteration return self.value def __iter__(self): return selfaList = list(aIter())print aList
- python 中的内置函数 与 类
- python中的内置函数总结
- python内置函数|类中的内置方法|字符串内置方法大全
- Python内置数据类型与函数
- 【python】类内置函数
- Python 中的三个内置函数__NOTE
- python内置函数repr()与eval()
- python内置函数与 javascript对照表
- python函数: 内置函数
- python基础-类内置函数、类内置方法
- thinkphp中的内置操作数据库与mysql中的函数汇总
- python 类的继承,内置函数(1)
- Python内置函数_数学运算类
- Python内置函数_集合操作类
- Python内置函数_逻辑判断类
- Python内置函数_反射类
- Python内置函数_IO操作类
- Python内置函数_集合操作类
- Windows DIB文件操作详解-1.DIB的读入、保存和显示
- 测试
- c语言,指针和数组大小之间的关系
- 2014百度之星资格赛第三题
- 如何实现抓取网站访客手机号的功能
- python 中的内置函数 与 类
- 交叉连接——CROSS JOIN
- 2014年江苏移动暑期实习笔试部分题目
- String池与iterator对集合的迭代
- java调用本地应用学习
- PHP的语法
- 若干个数 以“字符串形式‘从键盘输入,将其转换为 double 型,并求平均数
- 邻接表的两种遍历算法
- Win7系统安装PHPnow的解决方法