Python中类的属性、方法及内置方法
2018-01-10 23:11
435 查看
1.类的属性
成员变量
对象的创建
创建对象的过程称之为实例化,当一个对象被创建后,包含三个方面的特性对象聚丙属性和方法, 句柄用于区分不同的对象, 对象的属性和方法,与类中的成员变量和成员函数对应, obj = MyClass()创建类的一个实例,扩号对象,通过对象来调用方法和属性
类的属性
类的属性按使用范围分为公有属性和私有属性类的属性范围,取决于属性的名称, **共有属性**---在内中和内外都能够调用的属性 **私有属性**---不能在内外贝类以外函数调用 定义方式:以"__"双下划线开始的成员变量就是私有属性 可以通过instance.__classname__attribute方式访问, 内置属性--由系统在定义类的时候默认添加的由前后双下划线构成,如__dic__,__module__
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age ren = People() ren.color = '白色人' print ren.color ren.think() print '#'*30 print("People.color") print ren.__People__age ##测试时使用。如要调用 时,通过方法内调用 。
2.类的方法
成员函数 类的方法 方法的定义和函数一样,但是需要self作为第一个参数. 类方法为: 公有方法 私有方法 类方法 静态方法 公有方法:在类中和类外都都测调用的方法. 私有方法:不测被类的外部调用模块,在方法前加个“__”c双下划线就是私有方法。 self参数: 用于区分函数和类的方法(必须有一个self) self参数表示执行对象本身
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def test(self): self.think() # 内部调用 jack = People() jack.test() #外部调用
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(self): self.__talk() # 内部调用talk() jack = People() jack.test() #外部调用
类方法
类方法:被classmethod()函数处理过的函数,能被类所调用,也能被对象所调用(是继承的关系)。
静态方法:相当于“全局函数”,可以被类直接调用,可以被所有实例化对象共享,通过staticmethod()定义静态方法, 静态方法没有self参数 装饰器: @classmethod() @staticmethod()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(self): print 'Testing....' cm = classmethod(test) jack = People() People.cm()
通过类方法类内的方法 ,不涉及的属性和方法 不会被加载,节省内存,快。
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" def test(): ##没有self 静态调用 会把所有的属性加载到内存里。 print People.__age # 通过类访问内部变量 sm = staticmethod(test) jack = People() People.sm()
装饰调用类的方法:
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") jack = People() People.test() People.test1()
3.类的内置方法
Python内部类:
所谓内部类,就是在类的内部定义的类,主要目的是为了更好的抽象现实世界。 例子: 汽车是一个类,汽车的底盘轮胎也可以抽象为类,将其定义到汽车内中,而形成内部类, 更好的描述汽车类,因为底盘轮胎是汽车的一部分。
内部类实例化方法:
方法1:直接使用外部类调用内部类 方法2:先对外部类进行实例化,然后再实例化内部类 out_name = outclass_name() in_name = out_name.inclass_name() in_name.method()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): print("I am chinese") def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") jack = People.Chinese()
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") jack = People.Chinese() #外部类调用内部类 print jack.name #外部类调用内部类对象
另一种方法,外部类调用内部类对象 #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") ren = People() #实例化外部类 jack = ren.Chinese() #实例化内部类 print jack.name #打印内部类属性 或 print People.Chinese.name print People.Chinese().name
魔术方法:
str(self)构造函数与析构函数
构造函数:
用于初始化类的内部状态,Python提供的构造函数是__init__(): __init__():方法是可选的,如果不提供,python会给出一个默认的__init__方法。
析构函数:
用于释放对象占用的资源,python提供的析构函数是__del__(): __del__():也是可选的,如果不提供,则python会在后台提供默认析构函数。
构造函数__str__
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") ren = People() #实例化外部类 print ren #默认执行__str__
__init__(self)初始化类:
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #类实例化时自动执行 self.color = c self.think() def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") jack = People('green') ren = People() #实例化外部类 print ren.color #通过对象访问属性是初始化后的值 print People.color #通过类访问还是原来的值 [root@localhost 20180110]# python test1.py I am a black I am a thinker 30 black yellow
析构函数__del__():释放资源
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #类实例化时自动执行 print ("initing...") self.color = c self.think() f = open('test.py') def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") def __del__(self): print ("del....") self.f.close() jack = People('green') ren = People() #实例化外部类 print ren.color #通过对象访问属性是初始化后的值 print People.color #通过类访问还是原来的值
垃圾回收机制:
Python采用垃圾回收机制来清理不再使用的对象;python提供gc模块释放不再使用的对象。 Python采用“引用计数”的算法方式来处理回收,即:当然某个对象在其作用域内不再被其 他对象引用的时候,python就自动化清除对象。 gc模块collect()可以一次性收集所有待处理的对象(gc.collect)
#!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): color = 'yellow' __age = 30 #私有属性 class Chinese(object): name ="I am a Chinese." def __str__(self): return "This is People class" def __init__(self,c='white'): #类实例化时自动执行 print ("initing...") self.color = c self.think() f = open('test.py') def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method") def __del__(self): print ("del....") self.f.close() print gc.collect() 如果是0是没有回收的。 jack = People('green') ren = People() #实例化外部类 print ren.color #通过对象访问属性是初始化后的值 print People.color #通过类访问还是原来的值
相关文章推荐
- python3 面向对象__类的内置属性与方法的实例代码
- python的列表,元组以及字典的一些内置方法与属性
- python 方法属性(用户自定义方法,非内置方法)
- python类的属性、方法和内置方法
- python基础——内置方法
- Python第九天 面向对象 类定义 类的属性 类的方法 内部类 垃圾回收机制 类的继承 装饰器
- 全面了解python中的类,对象,方法,属性
- Python 3.x--面向对象编程(二)静态方法、类方法、属性方法
- python动态获取对象的属性和方法
- Python属性、方法和类管理系列之----描述符类
- python语言是动态语言、给类添加属性、方法、静态方法和类方法、__slots__()、python中的生成器、yield的用法
- 【转】[python] 类常用的内置方法
- python基础教程_学习笔记11:魔法方法、属性和迭代器
- js 内置对象属性及方法
- 菜鸟初识python request属性及方法说明
- python中的类,对象,方法,属性等介绍
- 浅谈python类属性的访问、设置和删除方法
- python通过__slots__限制类实例动态的增加属性、方法
- Python内置方法的时间复杂度(转)
- Python中的类属性和实例属性以及静态方法和类方法