The Python Tutorial之Class(二)

本文深入探讨了Python中的类对象、实例对象、方法对象的概念及其使用方式,并通过具体示例阐述了类变量与实例变量的区别。

###1. Class Objects

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

特别回顾上一篇的命名空间,class的定义开辟新的命名空间,该空间为local scope,但是从module的角度来看,class的name属于该module的global namespaces该结论并没有出处,是笔者自己的理解。

>>> class Complex:
...     def __init__(self, realpart, imagpart):
...         self.r = realpart
...         self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)

官网给出的例子,然后配合下面的说明理解class的定义:

  • Class objects support two kinds of operations: attribute references and instantiation.属性引用或者实例化
  • Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.有效的属性名字必须属于class对象的命名空间
  • Class attributes can also be assigned to, _doc_ is also a valid attribute, returning the docstring belonging to the class.class属性可以被赋值
  • Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class.
  • a class may define a special method named _init_(), class instantiation automatically invokes _init_() for the newly-created class instance.实例化的时候自动调用_init_()
  • Of course, the _init_() method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to _init_().实例化时传递的参数最终给_init_

2. Instance Objects

  • Data attributes need not be declared; like local variables, they spring into existence when they are first assigned to.class的数据属性并不需要在class的定义中声明,如下小实验验证:
>>> class change:
...     def sum(self):
...         z=1
...     i=1
...
>>> x=change()
>>> x.j=2
>>> x.j
2
>>>
  • The other kind of instance attribute reference is a method.
  • Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define corresponding methods of its instances.实例化之后,原来的函数对象变成该实例的method对象,两者是有区别的

3. Method Objects

  • In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s instance object before the first argument.x.sum()和change.sum(x)效果一样,理解这个需要理解深刻理解一切东西都是对象的含义
  • If you still don’t understand how methods work, a look at the implementation can perhaps clarify matters. When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object.method object生成的过程
  • When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.比较复杂的陈述了这个method调用的过程,该部分进一步解释上面x.sum()=change.sum(x)
4. Class and Instance Variables
class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

官网给的例子上的注释很清楚的解释了什么是Class and Instance Variables,然后又给出下面的例子:

class Dog:

    tricks = []             # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks                # unexpectedly shared by all dogs
['roll over', 'play dead']

如果从namespace思考的话,所有实例共享class Dog的命名空间,其trick被所有实例共享,而name只有实例自己可以访问,如果改动代码如下:

>>> class Dog:

    name = 'a' 

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)


>>> x=Dog('b')
>>> x.name
'b'
>>>

如果应用namespaces的规则,那么name首先在local scope查找,所以x.name=b而不是a

5. 总结

从内容编排来看介绍了Class Objects, Instance Objects, Method Objects,特别强调了object,最后又给出了Class and Instance Variables,该特性在之后的private详细讨论。总之理解上一篇的命名空间是基础,之后才能更深的理解其他概念,目前笔者感觉用The Python Tutorial入门是错误的决定。

【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)内容概要:本文提出了一种基于融合鱼鹰和柯西变异的麻雀优化算法(OCSSA)优化变分模态分解(VMD)参数,并结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)的轴承故障诊断模型。该方法利用西储大学公开的轴承数据集进行验证,通过OCSSA算法优化VMD的分解层数K和惩罚因子α,有效提升信号分解精度,抑制模态混叠;随后利用CNN提取故障特征的空间信息,BiLSTM捕捉时间序列的动态特征,最终实现高精度的轴承故障分类。整个诊断流程充分结合了信号预处理、智能优化与深度学习的优势,显著提升了复杂工况下轴承故障诊断的准确性与鲁棒性。; 适合人群:具备一定信号处理、机器学习及MATLAB编程基础的研究生、科研人员及从事工业设备故障诊断的工程技术人员。; 使用场景及目标:①应用于旋转机械设备的智能运维与故障预警系统;②为轴承等关键部件的早期故障识别提供高精度诊断方案;③推动智能优化算法与深度学习在工业信号处理领域的融合研究。; 阅读建议:建议读者结合MATLAB代码实现,深入理解OCSSA优化机制、VMD参数选择策略以及CNN-BiLSTM网络结构的设计逻辑,通过复现实验掌握完整诊断流程,并可进一步尝试迁移至其他设备的故障诊断任务中进行验证与优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值