Python之关于类变量的两种赋值区别详解
作者:叫我王员外就行 时间:2021-09-08 08:05:26
我就废话不多说了,还是直接看代码吧!
# -*- coding:utf-8 -*-
#面试题,写一个方法,将一行字符串中所有的单词数量统计出来
class Person(object):
TAG = "hello"
p1 = Person()
p2 = Person()
print p1.TAG #第一种赋值方式
print p2.TAG
print Person.TAG #第二种赋值方式
p1.TAG = "damn it"
print p1.TAG
print p2.TAG
print Person.TAG
输出结果:可以看到,只有p1的TAG,被赋值成了新的"damn it"
hello
hello
hello
damn it
hello
hello
如何将所有对象引用的类变量都赋值成同一个值呢?
Person.TAG = "damn it"
输出结果:完美解决问题
hello
hello
hello
damn it
damn it
damn it
补充知识:python类,赋值,命名空间
python中的类相当于一个命名空间,object.attr 就是一个向上爬属性的过程
属性:__dict__ , __class__ , __bases__
__dict__: 实例或类或模块的属性 , __class__ 实例对应的类对象,__bases__(元组):父类/超类
爬属性:
def findAttr(obj , attr):
if attr in obj.__dict__:
return obj.__dict__[attr]
cls = obj.__class__
if attr in cls.__dict__:
return cls.__dict__[attr]
for super_cls in cls.__bases__:
if attr in super_cls.__dict__:
return super_cls.__dict__[attr]
return None
爬类:
def classTree(cls,indent):
print('.'*indent + cls.__name__)
for super_cls in cls.__bases__:
classTree(super_cls,indent+4)
赋值方式:
class Test:
static_var = 1 #类属性相当与C++静态成员变量
def assign(self):
self.x = 1 #对象属性赋值
t = Test()
t.x = 2 #也可以这样 ,直接赋值
t.__dict__['x'] = 3 #也可以这样 , __dict__是对象空间词典,每个对象一份,类对象/模块也有
Test.add_static_var = 5 #也可以样新增一个类属性
类方法调用 , 常用的方式object.method() ,在python中扩展 Class.method(object) ,两者相同
Test.assign(t)
print(t.x)
当object.method() 时,object被传入method(self)中的第一个参数.Class.method(object) 需要手动传入
命名空间:
x = 0
def print_global():
print(x) #打印全局
def print_local():
x = 1
print(x) #本地变量
class A:
x = 2 #类属性==C++静态成员变量 , print A.x
def m(self):
x = 3 #本地变量
self.x = 4 #对象属性
def change_global():
global x #修改全局变量,否则x = 100 ,是增加一个本地变量
x = 100
def print_enclosing():
x = 200
def nested():
print(x) #在闭包中引用本地变量
def change_in_enclosing():
x = 1
def nested():
nonlocal x
x = 2 #在闭包中改变本地变量, 如没有nonlocal x , 在又新增一个本地变量
来源:https://blog.csdn.net/cadi2011/article/details/86438813
标签:Python,类变量,赋值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现印章代码的算法解析
2023-10-14 15:18:24
![](https://img.aspxhome.com/file/2023/7/131227_0s.png)
Python 忽略文件名编码的方法
2021-06-11 15:16:55
python http通信接口开发示例
2022-06-07 05:15:29
![](https://img.aspxhome.com/file/2023/9/96989_0s.png)
Python使用ClickHouse的实践与踩坑记录
2023-06-12 21:45:44
![](https://img.aspxhome.com/file/2023/3/59643_0s.jpg)
实例讲解Python中整数的最大值输出
2021-12-20 22:04:39
python GUI库图形界面开发之PyQt5拖放控件实例详解
2023-04-26 08:43:24
![](https://img.aspxhome.com/file/2023/9/64309_0s.png)
Pycharm配置autopep8实现流程解析
2021-08-23 05:34:40
![](https://img.aspxhome.com/file/2023/0/98850_0s.png)
Python中字典的基本知识初步介绍
2021-08-25 11:41:40
pycharm-professional-2020.1下载与激活的教程
2023-03-08 08:12:38
![](https://img.aspxhome.com/file/2023/9/97659_0s.png)
Python decimal模块的使用示例详解
2023-02-13 15:49:52
Python实现字典排序、按照list中字典的某个key排序的方法示例
2022-06-10 15:21:38
document.getElementById的简写方式
2010-06-21 10:44:00
python如何实现数据的线性拟合
2023-10-08 01:56:01
![](https://img.aspxhome.com/file/2023/7/95267_0s.jpg)
Python解释执行原理分析
2022-12-13 22:11:54
php session处理的定制
2023-11-15 18:38:12
解决Pyinstaller打包软件失败的一个坑
2022-05-31 21:47:14
TensorFlow2.0:张量的合并与分割实例
2022-12-02 13:44:49
Python批量修改文本文件内容的方法
2022-07-16 08:37:43
分享五个超实用Python脚本,减少垃圾软件负担
2022-07-18 18:38:59
![](https://img.aspxhome.com/file/2023/7/72767_0s.jpg)
关注oblog 关注xml-rpc 自己开发客户端小记
2009-10-04 20:27:00