python实例属性和类属性精讲
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实例属性和类属性精讲相关的知识,希望对你有一定的参考价值。
一:访问与改变
# -*-coding: UTF-8 -*-
class ShuXing(object):
class_shuxing = "class_shuxing"
class_shuxing_list = [1, 2, 3]
def __init__(self):
self.instance_shuxing = "instance_shuxing"
self.instance_shuxing_list = [5, 6, 7]
print ShuXing.class_shuxing
print ShuXing.class_shuxing_list
#print ShuXing.instance_shuxing 类名不能访问instance不可变变量
#print ShuXing.instance_shuxing_list 类名不能访问instance可变变量
a = ShuXing()
#instance可以访问任何变量
print a.class_shuxing
print a.class_shuxing_list
print a.instance_shuxing
print a.instance_shuxing_list
print "+++++++++++++++++++"
print "instance gaipian class"
a.class_shuxing = "gaipian class_shuxing"
a.class_shuxing_list.append("gaipian class_shuxing_list")
print ShuXing.class_shuxing #类不可变变量没改变
print ShuXing.class_shuxing_list #类可变变量有改变
#print ShuXing.instance_shuxing
#print ShuXing.instance_shuxing_list
a = ShuXing()
print a.class_shuxing
print a.class_shuxing_list #类可变变量有改变
print a.instance_shuxing
print a.instance_shuxing_list
print "+++++++++++++++++++"
print "class gaipian class"
ShuXing.class_shuxing = "gaipian class_shuxing"
ShuXing.class_shuxing_list.append("gaipian class_shuxing_list")
print ShuXing.class_shuxing #类不可变变量有改变
print ShuXing.class_shuxing_list #类可变变量有改变
#print ShuXing.instance_shuxing
#print ShuXing.instance_shuxing_list
a = ShuXing()
print a.class_shuxing #类不可变变量有改变
print a.class_shuxing_list #类可变变量有改变
print a.instance_shuxing
print a.instance_shuxing_list
1.Python是一门动态语言,任何实体都可以动态地添加或删除属性。
2.一个类定义了一个作用域。
3.类实例也引入了一个作用域,这与相应类定义的作用域不同。
4.在类实例中查找属性的时候,首先在实例自己的作用域中查找,如果没有找到,则再在类定义的作用域中查找。
5.在对类实例属性进行赋值的时候,实际上会在类实例定义的作用域中添加一个属性(如果还不存在的话),并不会影响到相应类中定义的同名属性。
class ShuXing2(object):
a = 0
def __init__(self):
self.a = 10
self.b = 20
print ShuXing2.a # 0
#print ShuXing2.b #类名不能访问instance变量
a = ShuXing2()
print a.a # 10
print a.b # 20
二:用方法访问变量
1 hasattr():根据输入参数判断某个容器中是否有已该参数为名称的内容,如果有则返回true
2 getattr():取出容器中以参数为名称的内容
3 setattr():对容器中以参数为名称的内容进行修改
4 delattr():删除容器中以参数为名称的内容
class ShuXing2(object):
a = 0
def __init__(self):
self.a = 10
self.b = 20
a = ShuXing2()
print hasattr(a, ‘a‘) #True
print getattr(a, ‘a‘) #10
print setattr(a, ‘a‘, 100) #None
print getattr(a, ‘a‘) #100
print delattr(a, ‘a‘) #None
print getattr(a, ‘a‘) #0
三:使用特性来管理类实例的属性访问控制
# -*- coding:UTF-8 -*-
class Student(object):
def __init__(self, score):
self.__score = score
@property
def score(self):
return self.__score
@score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError(‘score must be an integer!‘)
if value < 0 or value > 100:
raise ValueError(‘score must between 0 ~ 100!‘)
self.__score = value
s = Student(80)
#print s.__score
s.score = 60
print s.score
s.score = 9999
‘‘‘
60
Traceback (most recent call last):
File "lizi2.py", line 24, in <module>
s.score = 9999
File "lizi2.py", line 16, in score
raise ValueError(‘score must between 0 ~ 100!‘)
ValueError: score must between 0 ~ 100!
‘‘‘
本文出自 “大荒芜经” 博客,请务必保留此出处http://2892931976.blog.51cto.com/5396534/1750860
以上是关于python实例属性和类属性精讲的主要内容,如果未能解决你的问题,请参考以下文章
Python实用笔记 (22)面向对象编程——实例属性和类属性