Skip to content

python

Python function decorator

python function decorator post logo

This guide will show you how to create a Python function decorator with a few easy steps, to help you separate different concerns in code. This guide does not aim to provide reference material regarding decorators, but rather explain the basics and provide a starting point for implementing your own decorator.

What is a decorator?

A Python decorator is similar to a Java annotation if you are familiar with the JVM language, where you decorate or “annotate” a function prefixing the decorator’s name with @. For example, the decorator below publishes the function hello() on the url / on a Flask application:

@app.route('/')
def hello() -> str:
    return 'Hello world from Flask!'

More broadly speaking, a decorator is a structural design pattern that lets you add behaviour to an object, by wrapping its decorated function in a new function that has the same signature.

Read More »Python function decorator

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not be used when making a shared object.

When running some Python libs such as matplotlib or pymc, you might face and error similar to below:

libpython3.9.a(bytearrayobject.o): relocation R_X86_64_PC32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
---
E           /usr/bin/ld: /home/bgasparotto/.pyenv/versions/3.9.7/lib/libpython3.9.a(longobject.o): relocation R_X86_64_PC32 against symbol `PyExc_OverflowError' can not be used when making a shared object; recompile with -fPIC
E           /usr/bin/ld: final link failed: bad value
E           collect2: error: ld returned 1 exit status

In a nutshell, this error tells us that a Python shared lib expected to be available in your Python was not found during execution.

Read More »Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error