Fundamentals 16 min read

Avoid Common Python Pitfalls: A Practical Guide for C/C++ Migrants

This guide explains frequently confused Python operations, compares Python idioms with C/C++ conventions, introduces essential standard‑library tools, and offers performance‑oriented debugging techniques to help developers write cleaner, faster, and more reliable Python code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Avoid Common Python Pitfalls: A Practical Guide for C/C++ Migrants

1. Confusing Operations

This section compares several Python constructs that are easy to misuse.

1.1 Sampling with and without replacement

import random
random.choices(seq, k=1)   # list of length k, sampling with replacement
random.sample(seq, k)      # list of length k, sampling without replacement

1.2 Lambda parameter binding

func = lambda y: x + y          # x is bound at call time
func = lambda y, x=x: x + y     # x is bound at definition time

1.3 copy vs deepcopy

import copy
y = copy.copy(x)      # shallow copy (top level only)
y = copy.deepcopy(x)  # deep copy (all nested objects)

When combined with variable aliasing, copying can be confusing:

a = [1, 2, [3, 4]]
# Alias
b_alias = a
assert b_alias == a and b_alias is a
# Shallow copy
b_shallow = a[:]
assert b_shallow == a and b_shallow is not a and b_shallow[2] is a[2]
# Deep copy
import copy
b_deep = copy.deepcopy(a)
assert b_deep == a and b_deep is not a and b_deep[2] is not a[2]

Modifying an alias changes the original; elements in a shallow copy are still references to the original objects, while a deep copy is fully independent.

1.4 == vs is

x == y  # value equality
x is y  # identity (same object)

1.5 Type checking

type(a) == int      # ignores polymorphism
isinstance(a, int)  # respects inheritance

1.6 String searching

str.find(sub)   # returns -1 if not found
str.index(sub)  # raises ValueError if not found

1.7 List reverse indexing

Python allows negative indices; using the bitwise NOT operator (~) yields a zero‑based reverse index.

print(a[-1], a[-2], a[-3])
print(a[~0], a[~1], a[~2])

2. C/C++ User Guide

Many Python users come from C/C++; this section highlights syntactic and idiomatic differences.

2.1 Large and small numbers

a = float('inf')
b = float('-inf')

2.2 Booleans

a = True
b = False

2.3 Checking for None

if x is None:
    pass

Using if not x treats empty containers as false as well.

2.4 Swapping values

a, b = b, a

2.5 Chained comparisons

if 0 < a < 5:
    pass

2.6 Property getters/setters

Python supports @property but unnecessary abstraction can be 4‑5× slower than direct attribute access.

2.7 Function input/output parameters

C/C++ use pointers for output parameters and return status codes; Python raises exceptions directly for error handling.

2.8 File reading

with open(file_path, 'rt', encoding='utf-8') as f:
    for line in f:
        print(line)  # trailing 
 is preserved

2.9 Path joining

import os
os.path.join('usr', 'lib', 'local')

2.10 Argument parsing

Use argparse.ArgumentParser for a richer command‑line interface than manual sys.argv handling.

2.11 Invoking external commands

import subprocess
result = subprocess.check_output(['cmd', 'arg1', 'arg2']).decode('utf-8')
result = subprocess.check_output(['cmd', 'arg1', 'arg2'], stderr=subprocess.STDOUT).decode('utf-8')
result = subprocess.check_output('grep python | wc > out', shell=True).decode('utf-8')

2.12 "Batteries included" philosophy

Python provides built‑in solutions for many common problems, so avoid reinventing the wheel.

3. Common Tools

3.1 CSV I/O

import csv
# Read/write without header
with open(name, 'rt', encoding='utf-8', newline='') as f:
    for row in csv.reader(f):
        print(row[0], row[1])
with open(name, mode='wt') as f:
    writer = csv.writer(f)
    writer.writerow(['symbol', 'change'])
# Read/write with header
with open(name, mode='rt', newline='') as f:
    for row in csv.DictReader(f):
        print(row['symbol'], row['change'])
with open(name, mode='wt') as f:
    header = ['symbol', 'change']
    writer = csv.DictWriter(f, header)
    writer.writeheader()
    writer.writerow({'symbol': xx, 'change': xx})

For large CSV files, increase the field size limit:

import sys
csv.field_size_limit(sys.maxsize)

3.2 itertools utilities

import itertools
itertools.islice(iterable, start, stop)
itertools.filterfalse(pred, iterable)
itertools.takewhile(pred, iterable)
itertools.dropwhile(pred, iterable)
itertools.compress(iterable, selectors)
sorted(iterable)
itertools.groupby(sorted_iterable)
itertools.permutations(iterable, r)
itertools.combinations(iterable, r)
itertools.chain(*iterables)
import heapq
heapq.merge(*iterables)
zip(*iterables)
itertools.zip_longest(*iterables, fillvalue=None)

3.3 collections.Counter

import collections
cnt = collections.Counter(iterable)
most_common = cnt.most_common(n)
cnt.update(other_iterable)
cnt1 + cnt2   # addition
cnt1 - cnt2   # subtraction

3.4 defaultdict with default values

import collections
dd = collections.defaultdict(list)  # missing key gets an empty list

3.5 OrderedDict (preserves insertion order)

import collections
od = collections.OrderedDict(items)

4. High‑Performance Programming & Debugging

4.1 Error and warning output

import sys
sys.stderr.write('error message')
import warnings
warnings.warn('message', category=UserWarning)
# Control warning display via command line:
# python -W all   # show all warnings
# python -W ignore   # ignore all warnings
# python -W error   # turn warnings into exceptions

4.2 In‑code debugging

# Debug block executed only when not optimized
if __debug__:
    print('debug info')
# Run with -O to skip the block
# python -O script.py

4.3 Code style checking

pylint main.py

4.4 Profiling execution time

# Whole‑program profiling
python -m cProfile main.py
# Timing a specific block
from contextlib import contextmanager
from time import perf_counter
@contextmanager
def timeblock(label):
    start = perf_counter()
    try:
        yield
    finally:
        end = perf_counter()
        print(f"{label}: {end - start}")

with timeblock('counting'):
    pass

Optimization principles:

Focus on real bottlenecks, not the entire code base.

Avoid global variables; local lookups are faster.

Prefer direct attribute access over repeated self.member lookups.

Use built‑in data structures (list, dict, set) which are implemented in C.

Eliminate unnecessary intermediate objects and avoid copy.deepcopy().

Prefer ':'.join([...]) over repeated string concatenation.

5. Other Python Tricks

5.1 argmin / argmax

items = [2, 1, 3, 4]
argmin = min(range(len(items)), key=items.__getitem__)
# argmax is analogous

5.2 Transposing a 2‑D list

A = [['a11', 'a12'], ['a21', 'a22'], ['a31', 'a32']]
A_T = list(zip(*A))               # list of tuples
A_T = [list(col) for col in zip(*A)]  # list of lists

5.3 Reshaping a 1‑D list into a 2‑D list

A = [1, 2, 3, 4, 5, 6]
# Preferred method (pairs)
result = list(zip(*[iter(A)] * 2))
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingperformancePythonbest practicesStandard Library
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.