python变量的使用方式_对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与变量的使用方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,类(class)是一种数据类型,它定义了一组属性和方法。类的属性可以是变量或常量,方法则是函数。通过实例化类的对象,可以访问这些属性和方法。 以下是Python类的定义和使用的详细说明: 1. 定义类 在Python,定义类使用关键字`class`,语法如下: ``` class ClassName: '''类的文档字符串''' # 属性 attribute1 = value1 attribute2 = value2 # 方法 def method1(self): pass def method2(self, arg1, arg2): pass def __init__(self, arg1, arg2): pass ``` 其: - `ClassName`是类的名称,遵循Python的命名规范,首字母大写,采用驼峰命名法。 - `'''类的文档字符串'''`是对类进行描述的文档字符串,可以使用`help()`函数查看类的帮助信息。 - `attribute`是类的属性,可以是变量或常量,使用`attribute=value`的形式进行定义。 - `method`是类方法,是一组相关联的函数,必须包含`self`参数,`self`代表类的实例对象。 - `__init__`是类的构造函数,用于初始化对象的属性。 2. 实例化类 在定义类之后,可以创建类的实例对象。语法如下: ``` obj = ClassName(arg1, arg2) ``` 其,`ClassName`是类的名称,`arg1`和`arg2`是传递给构造函数`__init__`的参数。 3. 访问属性和方法 通过实例对象,可以访问类的属性和方法。语法如下: ``` # 访问属性 obj.attribute # 调用方法 obj.method() ``` 其,`attribute`是类的属性,`method`是类的方法。 4. 继承类 Python的类支持继承,即一个类可以继承另一个类的属性和方法。继承类使用关键字`super()`,语法如下: ``` class SubClass(ClassName): '''子类的文档字符串''' # 子类属性 sub_attr1 = value1 # 子类方法 def sub_method1(self): pass def __init__(self, arg1, arg2, arg3): super().__init__(arg1, arg2) self.sub_attr2 = arg3 ``` 其,`SubClass`是子类的名称,`ClassName`是父类的名称。子类可以添加自己的属性和方法,也可以重写父类的方法。在子类的构造函数使用`super().__init__()`调用父类的构造函数。 以上是Python类的定义和使用的详细说明。通过类,可以封装一组相关联的属性和方法,方便代码的管理和维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值