How to Fix SyntaxError: ‘return’ outside function in Python

SyntaxError return outside function in Python: Command Prompt output of a return line that lost its indentation

SyntaxError: ‘return’ outside function means Python found a return line that isn’t inside a def block. The fix is to indent it under the function, or, if there’s no function, replace it with break, print() or a variable: In this tutorial, I’ll reproduce the error five ways and fix each one, all run on Python … Read more >>

How to Use Class Decorators in Python

Python class decorators: Command Prompt output of a class used as a decorator counting calls

Python class decorators come in two kinds: a function placed above a class statement that receives the class and returns it (often changed), and a class whose instances wrap functions through __call__. Here’s the first kind in four lines: I tested each decorator here on Python 3.12.5, and the screenshots are straight from Command Prompt. … Read more >>

How to Get Function Name in Python (__name__ and inspect)

Get function name in Python: Command Prompt output of __name__ on a function, an alias, a lambda and a partial

The quickest way to get function name in Python code is the __name__ attribute. Every function object carries it as a plain string, so calculate_tax.__name__ returns ‘calculate_tax’: I ran every example below on Python 3.12.5 and took the screenshots in Command Prompt. Save each block as a .py file and run it with python file.py … Read more >>

How to Use the @property Decorator in Python

Python property decorator: Command Prompt output of a salary setter rejecting a negative value and a string

The Python property decorator turns a method into something you read like a plain attribute. Put @property above a method, and obj.name runs that method with no parentheses. Add @name.setter when you want to check values on assignment: Every output below came from Python 3.12.5 in Command Prompt on Windows 11. In this tutorial, we’ll … Read more >>

How to Exit a Function in Python

Exit a function in Python: Command Prompt output of guard clauses returning early from a discount function

To exit a function in Python, use return. It stops the function on that line and hands a value back to the caller; a bare return hands back None. Code after it never runs: In this tutorial, I’ll walk through every way out of a function I actually use, all run on Python 3.12.5 in … Read more >>

How to Use Static Variables in Python Functions

Python static variable in function: Command Prompt output of a function attribute counting orders

Python has no static keyword, so a plain local variable resets on every call. To create a static variable in Python function code, store the value on the function object itself as an attribute; it survives between calls: I pasted each snippet into a .py file and ran it with Python 3.12.5 from Command Prompt; … Read more >>

How to Use the map() Function in Python

Python map() function: Command Prompt output of map() with a lambda over two lists

The Python map() function calls a function on every item of an iterable and gives you the results one at a time. Write map(function, iterable), then wrap it in list() when you want a list back: For the record, I tested these snippets with Python 3.12.5 (and Python 3.14.8 for the newer strict= keyword) on … Read more >>

Difference Between Functions and Methods in Python

Difference between functions and methods in Python: Command Prompt output of a function object and a bound method

The difference between functions and methods in Python is ownership. A function stands on its own and you call it by name, while a method is a function defined inside a class that you call on an object, which Python passes in automatically as self: My test machine for this one: Python 3.12.5 on Windows … Read more >>

Python Built-in Functions: List and Examples

Python built-in functions: Command Prompt output listing every name in the builtins module

Python built-in functions are the ready-made tools such as print(), len(), sorted() and round() that come with the interpreter. You call them anywhere without an import, because Python loads them from the builtins module at startup: Before writing a word, I ran each snippet on Windows 11 under Python 3.12.5 (and checked the counts on … Read more >>