四时宝库

程序员的知识宝库

测开不得不会的python之类(class)的定义和使用

学习目录

1 什么是类

2 类的定义

2.1 类属性

2.2类方法

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

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

 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

Python3.7新特性:Data Class(python data class)

介绍

Python 3.7 中有一个新特性, 你可以使用一个装饰器 @dataclass来简化创建数据类的过程,新创建的数据类将自带有__init____repr__

数据类是一种用来存储数据的类,这种类往往不需要自定义的方法。通常,我们也管它叫数据结构。例如,一个存储点的三维坐标值的类,往往就只需要三个字段 (x, y, z)。

Python中如何深入理解class(python@classmethod)

类也是对象

在理解元类之前,你需要先掌握Python中的类。Python中类的概念借鉴于Smalltalk,这显得有些奇特。在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段。

对这方面感兴趣,想要学习python或者想要了解python可以加群:623715703

防患于未“燃”,让用电更智慧更安全

应急管理部消防救援局发布2020火灾情况:初步统计,全国共接报火灾25.2万起,死亡1183人,受伤775人,直接财产损失40.09亿元。其中,因短路、过载、接触不良等线路问题引发火灾占总数68.9%。

超燃训练大图!看舰艇编队协同铸利剑

来源:南海舰队

前不久,大连舰、长沙舰

柳州舰和岳阳舰组成编队

赴南海某海域开展实战化训练

“编指通报

多批低空慢速小目标

持续向我接近

编队各舰

根据责任扇面组织梯次抗击!”

云卷浪飞

一场编队协同防空演练拉开帷幕

《超燃之战》中你不知道的步坦协同战术

在《超燃之战》当中,坦克是一个神奇的定位,虽然坦克类型的英雄输出不高,但是却能够长时间的站在战场之上,死亡率很低。纵使其他英雄的输出再高,能够一直站在战场上,才能够笑到最后。很多小伙伴在选择坦克类型的英雄时,不重视小兵,认为他们的生存时间太短,也没什么伤害,但是,能够活用小兵的技能才能够在场上存活更长的时间。

硬核!武警特战队员人车协同训练现场超燃

近日,武警山西总队机动支队开展人车协同课目训练,全面提升特战队员综合作战能力。(杨帅辉)

来源: 央广军事微博

专家动态页面的实现——php基于CI框架的学习(二)

以下是本次学习的页面

网络安全之php站点被攻击的方式(php网站攻击实例)

这里列出16种php站点被攻击的方式

1、命令注入(Command Injection)

2、eval注入(Eval Injection)

3、客户端脚本攻击(Script Insertion)

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接