当前位置:科学 > 正文

理解 10 个最难的 Python 概念

2023-08-28 07:04:13  来源:启辰8


Python语法简单,易于学习,是初学者的理想选择。 然而,当您深入研究 Python 时,您可能会遇到一些难以理解的具有挑战性的概念。

在本文中,我将解释 10 个最难的 Python 概念,包括递归、生成器、装饰器、面向对象编程、线程、异常处理、*args 和 **kwargs、函数式编程、推导式和集合。


(资料图片仅供参考)

1. 递归

递归是一种编程技术,其中函数重复调用自身,直到满足特定条件。 在 Python 中,递归函数是在自己的代码块中调用自身的函数。

以下是 Python 中计算数字阶乘的递归函数的示例:

def factorial(n):    if n == 0:        return 1    else:        return n * factorial(n-1)

在此示例中,函数 Factorial() 接受参数 n 并检查它是否等于 0。 如果 n 为零,则返回 1,因为 0 的阶乘为 1。如果 n 不为零,则使用参数 n-1 调用自身,并将结果与 n 相乘。 这一直持续到 n 变为 0 并且函数返回最终结果。



2. 生成器

生成器是产生一系列结果的函数,但不创建列表。 它们比列表更节省内存。

具体来说,生成器是使用一种称为生成器函数的特殊函数创建的。 这些函数的定义与常规函数类似,但它们不返回值,而是使用“yield”关键字返回生成器对象。 然后,可以在循环中使用生成器对象,根据需要一次生成一个值。

def my_generator():    for i in range(10):        yield ifor num in my_generator():    print(num)# Output# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9

在此示例中,my_generator 函数生成从 0 到 9 的数字序列。调用该函数时,它返回一个生成器对象,可用于迭代序列的值。


3. 装饰器

装饰器是修改其他函数行为的函数。 装饰器最常见的用途之一是在不修改原始代码的情况下向现有函数添加功能。

在 Python 中,装饰器是将另一个函数作为参数、修改它并返回它的函数。 它们用“@”符号编写,后跟装饰器函数的名称。

def my_decorator(func):    def wrapper():        print("Before the function is called.")        func()        print("After the function is called.")    return wrapper@my_decoratordef say_hello():    print("Hello World!")# Call the decorated functionsay_hello()# Output:# Before the function is called.# Hello World!# After the function is called.

在此示例中,my_decorator 是一个装饰器函数,它将函数作为其参数并返回一个新的函数包装器。 包装函数在调用原始函数之前和之后添加了一些额外的功能。

@my_decorator 语法是将 my_decorator 函数应用于 say_hello 函数的简写方式。 当say_hello被调用时,实际上是调用my_decorator返回的包装函数,而my_decorator又调用了原来的say_hello函数。 这允许我们向 say_hello 函数添加额外的行为,而无需直接修改其代码。

4. 面向对象编程

Python 是一种面向对象编程(OOP)语言。 OOP 是一种编程范式,强调使用对象和类来组织和构造代码。

OOP 是通过使用类创建可重用代码来创建可重用代码。 对象本质上是类的实例,并且配备有定义其行为的属性(数据)和方法(函数)。

要在 Python 中创建类,请使用 class 关键字,后跟类的名称和冒号。 在类内部,您可以使用函数定义其属性和方法。

例如,假设我们要创建一个名为 Person 的类,它具有 name 属性和打印问候消息的greet 方法。 我们可以这样定义它:

class Person:    def __init__(self, name):        self.name = name
def greet(self):        print("Hello, my name is", self.name)# Create a new Person object with the name Johnperson = Person("John")print(person.name) # Johnperson.greet() # Hello, my name is John

5. 线程

线程是一种用于同时执行多个线程的技术。 它可以显着提高程序的性能。 这是一个例子:

import threadingdef print_numbers():    for i in range(1, 11):        print(i)def print_letters():    for letter in ["a", "b", "c", "d", "e"]:        print(letter)thread1 = threading.Thread(target=print_numbers)thread2 = threading.Thread(target=print_letters)thread1.start()thread2.start()thread1.join()thread2.join()print("Finished")

在此示例中,我们定义了两个函数 print_numbers 和 print_letters,它们分别简单地将一些数字和字母打印到控制台。 然后,我们创建两个 Thread 对象,将目标函数作为参数传递,并使用 start() 方法启动它们。 最后,我们使用 join() 方法等待两个线程完成,然后再将“Finished”打印到控制台。

6. 异常处理

异常处理是处理程序在执行过程中可能出现的运行时错误或异常的过程。 Python提供了一种机制来捕获和处理程序执行过程中出现的异常,保证程序即使出现错误也能继续运行。

try:    numerator = int(input("Enter numerator: "))    denominator = int(input("Enter denominator: "))    result = numerator / denominator    print("Result: ", result)except ZeroDivisionError:    print("Error: Cannot divide by zero!")except ValueError:    print("Error: Invalid input. Please enter an integer.")except Exception as e:    print("An error occurred:", e)

在此示例中,try 块包含可能引发异常的代码。 如果引发异常,它将被相应的 except 块捕获。

通过使用异常处理,我们可以处理代码中的错误并防止程序崩溃。 它向用户提供反馈,并根据发生的错误类型采取适当的操作。



7. *args 和 **kwargs

在 Python 中,*args 和 **kwargs 用于将未指定数量的参数传递给函数。 因此,在编写函数定义时,您不必知道将向函数传递多少个参数。

*args 用于将可变数量的非关键字参数传递给函数。 * 运算符用于将传递给函数的参数解包到元组中。 这允许您将任意数量的参数传递给函数。

def my_function(*args):    for arg in args:        print(arg)my_function("hello", "world", "!")# Output:# hello# world# !

**kwargs 用于将可变数量的关键字参数传递给函数。 ** 运算符用于将传递给函数的关键字参数解压缩到字典中。 这允许您将任意数量的关键字参数传递给函数。

def my_function(**kwargs):    for key, value in kwargs.items():        print(key, value)my_function(name="John", age=30, city="New York")# Output:# name John# age 30# city New York

8.函数式编程

函数式编程是一种强调使用函数来解决问题的编程范式。 Python 通过多个内置函数和特性(包括 lambda 函数、map()、filter() 和 reduce())提供对函数式编程的支持。

Lambda 是单行函数。

# A lambda function to square a numbersquare = lambda x: x**2# Using the lambda functionprint(square(5))   # Output: 25

map() 将函数应用于可迭代对象的每个元素,并返回一个包含结果的新可迭代对象。

nums = [1, 2, 3, 4, 5]squared_nums = list(map(lambda x: x**2, nums))print(squared_nums) # Output: [1, 4, 9, 16, 25]

filter() 创建一个函数返回 true 的元素列表。

nums = [1, 2, 3, 4, 5]even_nums = list(filter(lambda x: x % 2 == 0, nums))print(even_nums) # Output: [2, 4]

reduce() 以累积方式将函数应用于可迭代的元素并返回单个值。

from functools import reducenums = [1, 2, 3, 4, 5]product = reduce(lambda x, y: x*y, nums)print(product) # Output: 120


9. 推导式

在 Python 中,推导式是一种简洁高效的方法,通过对可迭代对象的元素应用转换或过滤条件来创建新列表、字典或集合。

列表推导式:

# Create a list of squares for the first 10 positive integerssquares = [x**2 for x in range(1, 11)]print(squares)# Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

字典:

# Create a dictionary with keys from a list and values as the length of each keyfruits = ["apple", "banana", "kiwi", "mango"]fruit_lengths = {fruit: len(fruit) for fruit in fruits}print(fruit_lengths)# Output: {"apple": 5, "banana": 6, "kiwi": 4, "mango": 5}

Set 推导式:

# Create a set of all even numbers between 1 and 20evens = {x for x in range(1, 21) if x % 2 == 0}print(evens)# Output: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

生成器推导式:

# Create a generator that yields the square of each number from 1 to 10squares_gen = (x**2 for x in range(1, 11))for square in squares_gen:    print(square)# Output:# 1# 4# 9# 16# 25# 36# 49# 64# 81# 100



10. 集合

在 Python 中,集合模块提供了内置类型的替代方案,这些替代方案更高效并提供附加功能,例如计数器、默认字典和命名元组。

集合模块中的 Counter 类用于计算列表中元素的出现次数。

from collections import Counterlst = ["apple", "banana", "cherry", "apple", "cherry", "cherry"]count = Counter(lst)print(count)# Output: Counter({"cherry": 3, "apple": 2, "banana": 1})print(count["cherry"])# Output: 3

Defaultdict 类是内置 dict 类的子类,它为缺失的键提供默认值。

from collections import defaultdictd = defaultdict(int)d["a"] = 1d["b"] = 2print(d["a"])# Output: 1print(d["c"])# Output: 0 (default value for missing keys is 0 because of int argument)

nametuple 类用于创建命名元组,它与常规元组类似,但字段被命名,从而更容易访问它们。

from collections import namedtuplePerson = namedtuple("Person", ["name", "age", "gender"])p1 = Person(name="Alice", age=30, gender="Female")p2 = Person(name="Bob", age=25, gender="Male")print(p1.name)# Output: Aliceprint(p2.age)# Output: 25

感谢您的阅读,我希望本文能够帮助您更好地理解最具挑战性的 Python 概念!

关键词:

推荐阅读

月壤形成的主要原因 月壤与土壤有什么区别

月壤形成的主要原因月壤形成过程没有生物活动参与,没有有机质,还极度缺水干燥;组成月壤的矿物粉末基本是由陨石撞击破砰形成,因此,粉末 【详细】

域名抢注是是什么意思?投资角度来看什么域名好?

域名抢注是是什么意思域名抢注是通过抢先注册的方式获得互联网删除的域名的使用权。域名是由点分隔的一串数字,用于标记一台计算机或一组计 【详细】

捷达保养费用是多少?捷达是哪个国家的品牌?

捷达保养费用是多少?全新捷达的保修期为2年或6万公里,以先到者为准,新车可享受一次免费保养,首次免费保养在5000-7500km或1年内进行。如 【详细】

天然气泄露会造成爆炸吗?天然气泄漏怎么办?

天然气泄露会造成爆炸吗?家里用的天然气如果泄露是会发生爆炸的。当空气中含有混合天然气时,在与火源接触的一系列爆炸危险中,就会发生爆 【详细】

四部门明确App收集个人信息范围 个人信息保护范围判断标准

四部门明确App收集个人信息范围近日,国家互联网信息办公室、工业和信息化部、公安部、国家市场监督管理总局联合印发《常见类型移动互联网 【详细】

关于我们  |  联系方式  |  免责条款  |  招聘信息  |  广告服务  |  帮助中心

联系我们:85 572 98@qq.com备案号:粤ICP备18023326号-40

科技资讯网 版权所有