Fundamentals 11 min read

Unlock Hidden Python Tricks: Variable Arguments, File Globbing, Debugging, and More

This article explores a collection of useful yet often overlooked Python features—including functions with arbitrary arguments, file searching with glob, debugging with inspect, generating unique IDs, serialization, compression, and registering shutdown hooks—providing clear code examples and practical explanations for each.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Unlock Hidden Python Tricks: Variable Arguments, File Globbing, Debugging, and More

After years of using Python, I discovered several useful functions and features that are often underutilized, so I compiled a list of Python capabilities you should know.

00. Functions with arbitrary number of arguments

Python allows optional parameters, but you can also define functions that accept any number of arguments using the *args syntax, which collects remaining arguments into a tuple.

def function(arg1="", arg2=""):
    print("arg1: {0}".format(arg1))
    print("arg2: {0}".format(arg2))

function("Hello", "World")
function()

Example of a function that accepts arbitrary arguments:

def foo(*args):
    # just use "*" to collect all remaining arguments into a tuple
    numargs = len(args)
    print("Number of arguments: {0}".format(numargs))
    for i, x in enumerate(args):
        print("Argument {0} is: {1}".format(i, x))

foo()
foo("hello")
foo("hello", "World", "Again")

General form: def fun(*args, **kwargs) where *args is a tuple of positional arguments and **kwargs is a dictionary of keyword arguments.

01. Using glob() to find files

The glob function works like an enhanced listdir(), allowing pattern‑based file searches.

import glob

# get all py files
files = glob.glob('*.py')
print(files)

You can search for multiple file types by combining patterns:

import itertools as it, glob

def multiple_file_types(*patterns):
    return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)

for filename in multiple_file_types('*.txt', '*.py'):
    print(filename)

To obtain absolute paths, combine glob with os.path.realpath():

import itertools as it, glob, os

def multiple_file_types(*patterns):
    return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)

for filename in multiple_file_types('*.txt', '*.py'):
    realpath = os.path.realpath(filename)
    print(realpath)

02. Debugging

The inspect module is handy for debugging; the example below shows basic usage together with the logging module.

import logging, inspect

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-4d: %(message)s',
                    datefmt='%m-%d %H:%M')
logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bow')

def test():
    frame, filename, line_number, function_name, lines, index = \
        inspect.getouterframes(inspect.currentframe())[1]
    print(frame, filename, line_number, function_name, lines, index)

test()

03. Generating unique IDs

Use the uuid module to create universally unique identifiers instead of misusing md5().

import uuid

result = uuid.uuid1()
print(result)

Note that the generated strings contain parts derived from the computer’s MAC address, which can cause similarity in the trailing characters. You can also create hashes with hmac or hashlib for different purposes.

import hmac, hashlib
key = '1'
data = 'a'
print(hmac.new(key, data, hashlib.sha256).hexdigest())

m = hashlib.sha1()
m.update("The quick brown fox jumps over the lazy dog")
print(m.hexdigest())

04. Serialization

Python’s built‑in pickle module can serialize complex objects to a byte stream.

import pickle

variable = ['hello', 42, [1, 'two'], 'apple']
file = open('serial.txt', 'w')
serialized_obj = pickle.dumps(variable)
file.write(serialized_obj)
file.close()

target = open('serial.txt', 'r')
myObj = pickle.load(target)
print(serialized_obj)
print(myObj)

For better interoperability, use json which encodes data to a text format compatible with JavaScript and many other languages.

import json

variable = ['hello', 42, [1, 'two'], 'apple']
encode = json.dumps(variable)
print("Encoded {} - {}".format(encode, type(encode)))

decoded = json.loads(encode)
print("Decoded {} - {}".format(decoded, type(decoded)))

05. Compressing strings

The zlib module can compress long strings, reducing size significantly.

import zlib

string = """   Lorem ipsum dolor sit amet, consectetur
        adipiscing elit. Nunc ut elit id mi ultricies
        ... (omitted for brevity) ..."""
print("Original Size: {}".format(len(string)))
compressed = zlib.compress(string)
print("Compressed Size: {}".format(len(compressed)))

decompressed = zlib.decompress(compressed)
print("Decompressed Size: {}".format(len(decompressed)))

06. Registering shutdown functions

The atexit module lets you run code automatically when a script terminates, even after an exception.

import atexit, time, math

def microtime(get_as_float=False):
    if get_as_float:
        return time.time()
    else:
        return '%f %d' % math.modf(time.time())

start_time = microtime(False)
atexit.register(start_time)

def shutdown():
    global start_time
    print("Execution took: {0} seconds".format(start_time))

atexit.register(shutdown)

Using atexit.register() ensures the registered functions run regardless of how the script exits, though they may not run if the process is killed abruptly.

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.

DebuggingPythonserializationCode Examplesfunctions
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

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.