杰瑞科技汇

python version 3.5

Of course. Python 3.5 was a significant release, marking a major step in the evolution of the language. It was released on September 13, 2025.

python version 3.5-图1
(图片来源网络,侵删)

Here's a comprehensive overview of Python 3.5, its key features, and its current status.

Summary of Python 3.5

Python 3.5 was a feature-rich release that introduced many powerful new capabilities, particularly in the areas of asynchronous programming, type hinting, and core language syntax. It solidified Python's position as a modern, versatile language suitable for both high-level scripting and systems programming.


Key New Features in Python 3.5

Asynchronous async and await Syntax (PEP 492)

This was arguably the most significant feature of Python 3.5. It introduced two new keywords, async and await, to make asynchronous code cleaner and more readable, building upon the existing asyncio library.

  • async def: Used to define a "coroutine function". When called, it returns a coroutine object instead of running immediately.
  • await: Used to pause the execution of a coroutine until the awaited coroutine (usually an I/O operation) is complete.

Before (in Python 3.4, using @asyncio.coroutine and yield from):

python version 3.5-图2
(图片来源网络,侵删)
import asyncio
@asyncio.coroutine
def old_fetch_data():
    print("Start fetching data...")
    # Simulate a network call
    yield from asyncio.sleep(2)
    print("Data fetched!")
    return "Here is your data"
# To run it
loop = asyncio.get_event_loop()
loop.run_until_complete(old_fetch_data())
loop.close()

In Python 3.5 (using async and await):

import asyncio
async def fetch_data():
    print("Start fetching data...")
    # Simulate a network call
    await asyncio.sleep(2)
    print("Data fetched!")
    return "Here is your data"
# To run it
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch_data())
loop.close()

This new syntax makes asynchronous code look and feel much more like synchronous code, dramatically improving its readability.

Variable Type Annotations (PEP 484)

While type hints were introduced in Python 3.0 with the typing module, Python 3.5 allowed you to add type information directly to function arguments and return values using a special syntax. This is primarily for static analysis tools (like MyPy) and linters (like Mypy, PyCharm's inspector) to catch type errors before runtime.

Example:

python version 3.5-图3
(图片来源网络,侵删)
def greeting(name: str) -> str:
    """Return a greeting string."""
    return "Hello, " + name
# Static checkers can now verify that you're passing a string
# greeting(123)  # MyPy would flag this as an error

This is not enforced by the Python interpreter at runtime (unless you use a library like typeguard), but it's invaluable for large, complex projects.

The typing Module and Generic Types

The typing module was significantly expanded in 3.5 to support more complex type hints, including generics, abstract base classes, and Union types.

  • List: For type-hinting lists.
  • Dict: For type-hinting dictionaries.
  • Tuple: For type-hinting tuples.
  • Union: For type-hinting a variable that can be one of several types.

Example:

from typing import List, Dict, Union
def process_items(items: List[Union[str, int]]) -> Dict[str, int]:
    """Process a list of items and return a dictionary."""
    return {str(item): len(str(item)) for item in items}
print(process_items(["apple", 123, "banana"]))
# Output: {'apple': 5, '123': 3, 'banana': 6}

Unpacking Generalizations (PEP 448)

This feature enhanced the unpacking capabilities of the operator. You could now use it in more places, making code more concise.

  • Unpacking in function calls:

    def my_function(a, b, c):
        print(f"a={a}, b={b}, c={c}")
    my_args = [1, 2, 3]
    my_function(*my_args)  # Was already possible
    # New: can unpack multiple times
    my_args_2 = [4, 5]
    my_function(*my_args, *my_args_2) # a=1, b=2, c=3, d=4, e=5 (if function had 5 args)
  • Unpacking in list/tuple/set literals:

    first, *rest, last = [1, 2, 3, 4, 5]
    print(rest)  # Output: [2, 3, 4]
    # New: can unpack into a list
    list_a = [1, 2, 3]
    list_b = [4, 5]
    combined = [*list_a, *list_b]
    print(combined) # Output: [1, 2, 3, 4, 5]

pathlib Module (Standard Library)

The pathlib module, which provides an object-oriented interface for filesystem paths, was introduced in Python 3.4 but became a standard library feature in 3.5 and was heavily promoted. It offers a much more intuitive and cross-platform way to handle file paths compared to the older os.path module.

Example:

from pathlib import Path
# Create a path object
p = Path('/home/user/documents') / 'report.txt'
# Check if it exists
if p.exists():
    # Read the file content
    content = p.read_text()
    print(f"File content: {content[:50]}...")

Matrix Multiplication Operator () (PEP 465)

A new infix operator, , was introduced specifically for matrix multiplication. This was a huge win for the scientific and data science communities, as it provided a clear and unambiguous syntax for a very common mathematical operation.

Example:

import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Matrix multiplication
c = a @ b
print(c)
# Output:
# [[19 22]
#  [43 50]]

Security and Deprecation Notices

  • SSL Context Defaults: The default SSL/TLS settings were made more secure. The default protocol was changed from the insecure PROTOCOL_SSLv23 to PROTOCOL_TLS, and the insecure ssl.wrap_socket() function was deprecated in favor of SSLContext.wrap_socket().
  • Deprecated distutils: The distutils module was officially deprecated. It was the predecessor to setuptools and was long considered obsolete. Modern packaging should now rely on setuptools and build.
  • Deprecated imp Module: The imp module was deprecated in favor of the importlib module, which is the modern, more flexible way to handle dynamic imports.

Current Status and Recommendation

Python 3.5 is End-of-Life (EOL).

This means it no longer receives security updates, bug fixes, or other improvements. It is strongly recommended that you do not use Python 3.5 in any new projects.

Reasons to upgrade:

  1. Security: You will be vulnerable to known security flaws that have been patched in newer versions.
  2. Performance: Newer versions of Python are significantly faster.
  3. Features: You will miss out on many important improvements and new features introduced in Python 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, and 3.12.
  4. Community Support: Many third-party libraries have dropped support for Python 3.5, making it difficult to use the latest tools.

If you are currently using Python 3.5, you should plan to migrate to a supported version, such as Python 3.8, 3.9, or 3.10, which are long-term support (LTS) releases.

分享:
扫描分享到社交APP
上一篇
下一篇