对python 中class与变量的使用方法详解

 更新时间:2019年06月26日 09:56:40   作者:lawenliu  
今天小编就为大家分享一篇对python 中class与变量的使用方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

python中的变量定义是很灵活的,很容易搞混淆,特别是对于class的变量的定义,如何定义使用类里的变量是我们维护代码和保证代码稳定性的关键。

#!/usr/bin/python
#encoding:utf-8
 
global_variable_1 = 'global_variable'
 
class MyClass():
  class_var_1 = 'class_val_1' # define class variable here
  def __init__(self, param):
    self.object_var_1 = param # define object variable here
    self.object_var_2 = 'object_val_2' # define object variable here
    self.object_func3()
 
  def object_func1(self, param):
    local_var_1 = param # define lcoal variable here
    local_var_2 = 'local_val_2' # define local variable here
    self.internal_var_1 = 'internal_val_1' # define internal variable here
    print(local_var_1) # we can use local variable of current here
    print(local_var_2) # we can use local variable of current here
    print(MyClass.class_var_1) # we can use class variable here, but you have using class name ass prefix
    print(self.class_var_1) # we can use class variable as object variable here
    print(self.object_var_1) # we can use object variable here
    print(self.object_var_2) # we can use object variable here
    print(self.internal_var_1) # we can use internal variable here
    #print(local_var_3) # we can't use local variable in another function
    print(global_variable_1) # we can use global variable here
 
  def object_func2(self, param='func_val_1'):
    local_var_3 = param # define local variable here
    print(local_var_3) # we can use lcoal variable here
    print(self.internal_var_1) # we can use internal variable defined in class_func1, but you have to call class_func1 first
    print(MyClass.class_var_1) # we can use class variable here, but you have using class name ass prefix
    print(self.class_var_1) # we can class variable here
    print(self.object_var_1) # we can use object variable here
    print(self.object_var_2) # we can use object variable here
    print(global_variable_1) # we can use global variable here
 
 
  def object_func3(self, param='func_val_1'):
    self.object_var_3 = param # because this function called in construction function, so this is defined as object variable, not internal variable
    self.object_var_4 = 'object_val_4' # because this function called in construction function, so this is defined as object variable, not internal variable
    print(global_variable_1) # we can use global variable here
  
  # define class function
  def class_func4():
    print(MyClass.class_var_1)
    print(global_variable_1) # we can use global variable here
 
if __name__ == '__main__':
  myObject = MyClass('object_val_1')
  print(MyClass.class_var_1) # we can use class variable directly here
  #print(MyClass.object_var_1) # we can't use object variable here
  print(myObject.object_var_1) # we can use object variable here
  print(myObject.object_var_2) # we can use object variable here
  print(myObject.object_var_3) # we can use object variable here
  print(myObject.object_var_4) # we can use object variable here
  #print(myObject.internal_var_1) # we can't use internal variable as object variable here
  MyClass.class_func4() # we can use class function here
  #MyClass.object_func2(myObject, 'local_var_3') # internal variable can't be used in this function
  myObject.object_func1('local_var_1') # call first function
  myObject.object_func2('local_var_3') # call second function
  print(global_variable_1) # we can use global variable here

简单的写了个测试小程序,枚举了各种情况,没有办法全部枚举,但大部分情况应该都已经包含了。

1. 类变量:能够通过类名或者object的self来访问到,在类的内部和外部均可达,比如class_var_1

2. 对象变量:可以通过对象的self来使用的变量,通过constructor一路走向去的的self初次被赋值的变量都会成为对象变量,比如object_var_1, object_var_2, object_var_3, object_var_4

3. 内部变量:可以在函数中定义,并加上self前缀,在初次调用过定义的函数后,就可以在后面的对象的函数中被使用,比如internal_var_1

4. 局部变量:在函数内部定义,并使用的变量,在使用完之后就会被回收对类及object不可见

5. 全局变量:定义在类或者函数外部,作用域在变量被定义之后的任意代码段,比如:global_var_1

以上是基于我自己的测试得到的结论,如果有不对的地方,可以帮忙指正。

这篇对python 中class与变量的使用方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python内置函数之callable函数解读

    Python内置函数之callable函数解读

    这篇文章主要介绍了Python内置函数之callable函数的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-07-07
  • PyQt5 实现给无边框widget窗口添加背景图片

    PyQt5 实现给无边框widget窗口添加背景图片

    这篇文章主要介绍了PyQt5 实现给无边框widget窗口添加背景图片的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • python库Tsmoothie模块数据平滑化异常点抓取

    python库Tsmoothie模块数据平滑化异常点抓取

    这篇文章主要为大家介绍了python库Tsmoothie模块数据平滑化技术实现异常点抓取,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Pandas实现复制dataframe中的每一行

    Pandas实现复制dataframe中的每一行

    这篇文章主要介绍了Pandas实现复制dataframe中的每一行方式,
    2024-02-02
  • Python中PyAutoGUI 库的使用

    Python中PyAutoGUI 库的使用

    PyAutoGUI 是 Python 生态中 ​功能最全面的图形界面自动化库,通过模拟鼠标、键盘操作和屏幕图像识别,本文就来介绍一下Python中PyAutoGUI库的使用,感兴趣的可以了解一下
    2025-04-04
  • PyTorch CUDA环境配置及安装的步骤(图文教程)

    PyTorch CUDA环境配置及安装的步骤(图文教程)

    这篇文章主要介绍了PyTorch CUDA环境配置及安装的步骤(图文教程),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Python 代码性能优化技巧分享

    Python 代码性能优化技巧分享

    选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化来提高程序的执行效率
    2012-08-08
  • python如何开启多线程

    python如何开启多线程

    这篇文章主要介绍了python如何开启多线程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Python实现翻转数组功能示例

    Python实现翻转数组功能示例

    这篇文章主要介绍了Python实现翻转数组功能,涉及Python针对数组的遍历、判断、排序等相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • 在 Python 中使用变量创建文件名的方法

    在 Python 中使用变量创建文件名的方法

    这篇文章主要介绍了在 Python 中使用变量创建文件名,格式化的字符串文字使我们能够通过在字符串前面加上 f 来在字符串中包含表达式和变量,本文给大家详细讲解,需要的朋友可以参考下
    2023-03-03

最新评论