Overwritting print function in Python

By Piotr Sikora

  • 6 September 2025

Did you knew that you can easily overwrite print function using FunctType? Use code below to see how it works.

import random
def random_str():
    return random.choice([
        'first', 'second', 'third'
    ])

def alt_print(s):
    output = []
    for ch in s:
        c = ord(ch)
        if c in range(ord('a'), ord('z')+1):
            c += 119945
        elif ord('A') <= c <= ord('Z'):
            c += 119951
        output.append(chr(c))
        output.append(' ')
    print(''.join(output))


def firstFunction(s="test"):
    print(s)

import types
myFirstFunction = types.FunctionType(
    firstFunction.__code__,
    {
        "print": alt_print
    },
    name="myFirstFunction",
    argdefs=("Function",) # <= there is "," in the end - IT HAS TO BE THERE
)

myFirstFunction()

Categories

Recent Posts

About Me

Piotr Sikora - Process Automation | AI | n8n | Python | JavaScript

Piotr Sikora

Process Automation Specialist

I implement automation that saves time and money, streamlines operations, and increases the predictability of results. Specializing in process automation, AI implementation, and workflow optimization using n8n, Python, and JavaScript.

n8n Workflows

n8n workflow automation templates

Explore my workflow templates on n8n. Ready-to-use automations for blog management, data collection, and AI-powered content processing.

3Workflow Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Similar Articles

Discover more related content

XOR Cipher Implementation: Encryption in Python

XOR Cipher Implementation: Encryption in Python

Explore XOR cipher implementation in Python. Learn how this encryption algorithm works with practical code examples and analysis.