search
HomeBackend DevelopmentPython TutorialSome programming advice for Python beginners

Python is a very expressive language. It provides us with a huge standard library and many built-in modules to help us get our work done quickly. However, many people may get lost in the features it provides, fail to take full advantage of the standard library, place too much emphasis on one-line scripts, and misunderstand the basic structure of Python. This article is a non-exhaustive list of some of the pitfalls new Python developers can fall into.

1. Don’t know the Python version

This is a recurring question on StackOverflow. Many people can write code that works perfectly on one version, but they have a different version of Python installed on their system. Make sure you know which version of Python you are using.

You can check the Python version through the code below:

[pythontab@testServer]$ python --version
Python 2.7.10
[pythontab@testServer]$ python --V
Python 2.7.10

Both of the above methods are possible

2. Do not use a version manager

pyenv is an excellent tool for managing different Python versions, but Unfortunately, it only works on *nix systems. On Mac systems, you can simply install it via brew install pyenv, and on Linux, there is an automatic installer as well.

3. Addicted to one-line programs

Many people are keen on the excitement brought by one-line programs. Even if their one-line solution is less efficient than a multi-line solution, they'll brag about it.

A one-liner in Python essentially means a complex derivation with multiple expressions. For example:

l = [m for a, b in zip(this, that) if b.method(a) != b for m in b if not m.method(a, b) and reduce(lambda x, y: a + y.method(), (m, a, b))]

To be honest, I made up the above example. But I see a lot of people writing similar code. Code like this becomes unintelligible after a week. If you want to do something slightly more complex, such as simply adding an element to a list or set based on a condition, you're probably going to make a mistake.

Singles of code are not an achievement, yes they may look flexible, but they are not an achievement. Imagine it's like you're cleaning your house and stuffing everything into your closet. Good code should be clean, easy to read and efficient.

4. Initializing a collection in the wrong way

This is a more subtle problem that may catch you off guard. Set comprehensions are a lot like list comprehensions.

 
>>> { n for n in range(10) if n % 2 == 0 }
{0, 8, 2, 4, 6}
>>> type({ n for n in range(10) if n % 2 == 0 })

The above is an example of set derivation. A collection is like a list and a container. The difference is that there cannot be any duplicate values ​​in a set and it is unordered. People who see set derivation often mistakenly think that {} initializes an empty set. But it doesn't, it initializes an empty dictionary.

>>> {}
{}
>>> type({})

If you want to initialize an empty collection, you can simply call the set() method.

>>> set()
set()
>>> type(set())

Note that an empty set is represented by set(), but a set containing some elements must be represented by curly braces surrounding the elements.

>>> s = set()
>>> s
set()
>>> s.add(1)
>>> s
{1}
>>> s.add(2)
>>> s
{1, 2}

This is counter-intuitive because you expect something similar to set([1, 2]).

5. Misunderstanding GIL

GIL (global interpreter lock) means that in a Python program, only one thread can be running at any point in time. This means that when we create a thread and want it to run in parallel, it doesn't do that. The actual job of the Python interpreter is to quickly switch between different running threads. But this is a very simple explanation of what actually happens, which is much more complex. There are many examples of running in parallel, for example using various libraries that are essentially C extensions. But when you run Python code, most of the time it doesn't execute in parallel. In other words, threads in Python are not like threads in Java or C++.

Many people will try to defend Python by saying these are real threads. This is certainly true, but it doesn't change the fact that Python handles threads differently than you might expect. The same situation exists with the Ruby language (Ruby also has an interpreter lock).

The specified solution is to use the multiprocessing module. The multiprocessing module provides the Process class, which is a good overlay for fork. However, a fork process is much more expensive than a thread, so you may not see a performance improvement every time because a lot of work needs to be done between different processes to coordinate with each other.

However, this problem does not exist in every implementation of Python. For example, one implementation of Python, PyPy-stm, attempts to get rid of the GIL (it is still unstable). Python implementations built on other platforms, such as the JVM (Jython) or the CLR (IronPython), also have no GIL issues.

In short, be careful when using the Thread class, what you get may not be what you want.

6. Use old-style classes

在Python 2中,有两种类型的类,分别为“旧式”类和“新式”类。如果你使用Python 3,那么你默认使用“新式”类。为了确保在Python2中使用“新式”类,你需要让你新创建的每一个类都继承object类,且类不能已继承了内置类型,例如int或list。换句话说,你的基类、类如果不继承其他类,就总是需要继承object类。

class MyNewObject(object):
# stuff here

   

这些“新式”类解决一些老式类的根本缺陷,想要详细了解新式类和旧式类请参见《python新式类和旧式类区别》《python2中的__new__与__init__,新式类和经典类》。

7.按错误的方式迭代

对于这门语言的新手来说,下边的代码是非常常见的:

for name_index in range(len(names)):
print(names[name_index])

   

在上边的例子中,没有必须调用len函数,因为列表迭代实际上要简单得多:

for name in names:
print(name)

   

此外,还有一大堆其他的工具帮助你简化迭代。例如,可以使用zip同时遍历两个列表:

for cat, dog in zip(cats, dogs):
print(cat, dog)

如果你想同时考虑列表变量的索引和值,可以使用enumerate:

   
for index, cat in enumerate(cats):
print(cat, index)

在itertools中也有很多有用的函数供你选择。然而请注意,使用itertools函数并不总是正确的选择。如果itertools中的一个函数为你试图解决的问题提供了一个非常方便的解决办法,例如铺平一个列表或根据给定的列表创建一个其内容的排列,那就用它吧。但是不要仅仅因为你想要它而去适应你代码的一部分。

滥用itertools引发的问题出现的过于频繁,以至于在StackOverflow上一个德高望重的Python贡献者已经贡献他们资料的重要组成部分来解决这些问题。

8.使用可变的默认参数

我多次见到过如下的代码:

def foo(a, b, c=[]):
# append to c
# do some more stuff

永远不要使用可变的默认参数,可以使用如下的代码代替:

def foo(a, b, c=None):
if c is None:
c = []
# append to c
# do some more stuff

   

与其解释这个问题是什么,不如展示下使用可变默认参数的影响:

>>> def foo(a, b, c=[]):
... c.append(a)
... c.append(b)
... print(c)
...
>>> foo(1, 1)
[1, 1]
>>> foo(1, 1)
[1, 1, 1, 1]
>>> foo(1, 1)
[1, 1, 1, 1, 1, 1]

同一个变量c在函数调用的每一次都被反复引用。这可能有一些意想不到的后果。

总结

这些只是相对来说刚接触Python的人可能会遇到的一些问题。然而请注意,可能会遇到的问题远非就这么些。然而另一些缺陷是人们像使用Java或C++一样使用Python,并且试图按他们熟悉的方式使用Python。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software