Python启航:30天编程速成之旅(第12天)- 类

liftword4个月前 (01-07)技术文章41

喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。

前期基础教程:

「Python3.11.0」手把手教你安装最新版Python运行环境

讲讲Python环境使用Pip命令快速下载各类库的方法

Python启航:30天编程速成之旅(第2天)-IDE安装

【Python教程】JupyterLab 开发环境安装


Python启航:30天编程速成之旅(第12天)- 类

所有代码都是我本人实际编写并运行、截图,并标注详细的注释。

在 Python 中,类(Class)是面向对象编程(Object-Oriented Programming, OOP)的一个基本组成部分。类是一种用户定义的数据类型,它允许创建具有相同属性(数据成员)和方法(函数)的对象集合。

范围和命名空间示例

我们来用一个例子演示 local, nonlocal, 和 global 关键字是如何工作的。我们将构造一个简单的例子,其中包含一个外部函数,该函数定义了一个变量,并包含了几个内部函数来展示这些关键字的行为。

def outer_function():
    message = 'Hello from outer function!'  # 外部函数中的变量
    
    def inner_function_one():
        # 局部变量,只在这个函数内有效
        message = 'Hello from inner function one!'
        print('Inner Function One:', message)
    
    def inner_function_two():
        nonlocal message
        message = 'Hello from inner function two!'
        print('Inner Function Two:', message)
    
    def inner_function_three():
        global message
        message = 'Hello from inner function three!'
        print('Inner Function Three:', message)

    # 在调用任何内部函数之前,打印外部函数的 message
    print('Before any inner function calls:', message)
    
    # 调用第一个内部函数
    inner_function_one()
    
    # 在调用第二个内部函数前,打印 message 的值
    print('Between inner functions:', message)
    
    # 调用第二个内部函数
    inner_function_two()
    
    # 在调用第三个内部函数前,打印 message 的值
    print('Before global modification:', message)
    
    # 调用第三个内部函数
    inner_function_three()
    
    # 在所有内部函数调用后,打印 message 的值
    print('After all inner function calls:', message)

# 在外部函数外定义一个全局的 message 变量
message = 'Hello from global scope!'

# 调用外部函数
outer_function()

# 在所有函数调用之后,打印全局的 message
print('In global scope after all function calls:', message)

从上述代码的执行结果我们可以看到以下几点:

  1. 局部变量:当 inner_function_one() 被调用时,它创建了一个局部变量 message,并且这个变量仅存在于该函数的作用域内。因此,inner_function_one() 内的 message 不会影响外部的 message。
  2. 非局部变量:当 inner_function_two() 被调用时,使用了 nonlocal 关键字来引用外部函数 outer_function 的 message 变量,并对其进行了修改。因此,inner_function_two() 的输出反映了 outer_function 中 message 的变化,并且在 inner_function_two() 之后再次打印 message 时,可以看到它的值已经更新为 Hello from inner function two!。
  3. 全局变量:当 inner_function_three() 被调用时,使用了 global 关键字来引用全局作用域中的 message 变量,并对其进行了修改。这意味着 inner_function_three() 修改的是全局 message 的值,而不是 outer_function 中的局部 message。因此,在 outer_function 内部 inner_function_three() 之后打印的 message 仍然是 outer_function 的 message,而在全局作用域中打印的 message 显示为 Hello from inner function three!。

总结一下输出:

  • 在调用任何内部函数之前,outer_function 中的 message 为 Hello from outer function!。
  • 调用 inner_function_one() 后,其内部的 message 显示为 Hello from inner function one!,但对外部无影响。
  • 在 inner_function_one() 和 inner_function_two() 之间,outer_function 的 message 保持不变。
  • 调用 inner_function_two() 后,outer_function 中的 message 更新为 Hello from inner function two!。
  • 调用 inner_function_three() 后,outer_function 中的 message 保持为 Hello from inner function two!,但全局 message 被修改为 Hello from inner function three!。
  • 最终,在全局作用域中打印的 message 显示为 Hello from inner function three!。

喜欢的条友记得关注、点赞、转发、收藏,你们的支持就是我最大的动力源泉。

相关文章

一、Python 中类的定义、创建、使用

Python 界的领袖 Tim Peters 说的:元类就是深度的魔法,99% 的用户应该根本不必为此操心。如果你想搞清楚究竟是否需要用到元类,那么你就不需要它。那些实际用到元类的人都非常清楚地知道他...

Python中的类对象、类属性、类方法与实例对象、实例属性

类对象:类名所对应的内存空间类属性:所有的类对象和实例对象都可以直接访问类方法:第一个形参是类对象cls,其上方有@classmethod,可获取或修改类属性的数据实例对象:变量名所对应的内存空间实例...

Python类的定义、封装、继承和多态

# 定义一个基础类 Animal,用于表示动物 class Animal: # 构造函数,用于初始化动物的名称和年龄 def __init__(self, name, age):...