惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
博客园_首页
博客园 - 叶小钗
腾讯CDC
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
D
Docker

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Quark's Outlines: Python User-defined Functions
Mike Vincent · 2026-05-30 · via DEV Community

Quark’s Outlines: Python User-Defined Functions

Overview, Historical Timeline, Problems & Solutions

An Overview of Python User-Defined Functions

What is a Python user-defined function?

You can define your own function in Python using the def keyword. A Python user-defined function is made when you write a def block in your code. When Python runs this block, it creates a function object.

A function object has special parts. These include its name, its list of default values, and its code. It also keeps a link to the global names from the file where it was made. You can use this object to call the function later with any valid input.

Python lets you create named function objects with the def keyword.

def greet(name="friend"):
    return "Hello, " + name

print(greet())
print(greet("Mike"))
# prints:
# Hello, friend
# Hello, Mike

The function greet is now a user-defined function. Python stores its name, code, and default values for later use.

What are the special parts of a Python function?

A Python function holds many facts about itself. These are called attributes. For example, __name__ holds the function name. __defaults__ is a tuple of default values. __globals__ holds the global names it can see. __code__ is a special object that stores the function’s bytecode.

Python functions store their details in special attributes.

def square(x=2):
    return x * x

print(square.__name__)
print(square.__defaults__)
print(square.__code__.co_varnames)
# prints:
# square
# (2,)
# ('x',)

These parts let Python understand and run your function later.


A Historical Timeline of Python User-Defined Functions

Where do Python’s user-defined functions come from?

User-defined functions in Python build on ideas from earlier languages. They let you name blocks of code and reuse them. Over time, Python added new parts to function objects—like closures, default values, and metadata—to make them more powerful.


People designed ways to name and reuse logic

1958 —Functions in LISP let users define reusable logic blocks for the first time.

1972 —Procedures in C gave a fixed way to declare and call code blocks with arguments.


People added first-class functions to Python

1991 —Python adds def keyword, making user-defined function objects.

1994 —Python functions gain __name__ and __doc__ attributes.

2000 —Nested functions and closures added with lexical scoping in Python 2.1.

2004 —__code__ object exposed to let functions be inspected.

2015 —Function annotations added for argument hints and return types.

2025 —Function structure stable, with introspection, closures, and metadata all supported.


Problems & Solutions with Python User-Defined Functions

How do you use Python user-defined functions the right way?

Python functions are blocks of code you can name and reuse. These examples show how functions are stored, how their defaults work, how you can inspect them, and how to understand what happens when they are created and called.


Problem: How do you reuse the same logic with different inputs in Python?

You want to avoid repeating the same code. You want to give it a name, reuse it, and pass in different values each time. You need to define a function and call it when needed.

Problem: You want to make code reusable and easy to change.

Solution: Python lets you define a function using def and call it with arguments.

Python lets you reuse logic by defining and calling a function.

def add(x, y):
    return x + y

print(add(2, 3))
print(add(10, -5))
# prints:
# 5
# 5

The add function gives a simple way to reuse addition logic with any numbers.


Problem: How do you define a function with default values in Python?

You want to make a function that works with or without an input. If no value is given, it should still run using a fallback value. You want a safe default when nothing is passed.

Problem: You want a function to work even if no input is given.

Solution: Python lets you set default argument values in your function.

Python lets you define functions with default argument values.

def greet(name="friend"):
    return "Hello, " + name

print(greet())
print(greet("Sam"))
# prints:
# Hello, friend
# Hello, Sam

The function uses the default value "friend" unless another name is given.


Problem: How do you check what a function holds in Python?

You want to look inside a function and see what Python remembers. You want to see its name, its default values, and the code it will run. You want to inspect the function object.

Problem: You need to understand what a function knows about itself.

Solution: Python gives function objects special attributes you can inspect.

Python lets you inspect functions using built-in attributes.

def square(x=3):
    return x * x

print(square.__name__)
print(square.__defaults__)
print(square.__code__.co_varnames)
# prints:
# square
# (3,)
# ('x',)

These attributes show the function name, default values, and variable names.


Problem: How do you store a function for later use in Python?

You want to save a function inside a variable or pass it to another function. You do not want to run it yet. You only want to store the logic until later.

Problem: You want to treat functions as values and pass them around.

Solution: Python lets you store function objects in variables or pass them to others.

Python lets you assign function objects to names and pass them around.

def shout(word):
    return word.upper() + "!"

s = shout
print(s("hello"))
# prints:
# HELLO!

You can treat the shout function like any value and use it under another name.


Problem: How do you check a function's global names in Python?

You are inside a function and want to know which global names it can see. You want to inspect where it was defined and what global values it uses. This helps when debugging or writing tools.

Problem: You want to trace which global names a function depends on.

Solution: Python stores the global dictionary in the function’s __globals__.

Python lets you inspect the global scope used by a function.

message = "Hi"

def say():
    return message

print(say.__globals__['message'])
# prints:
# Hi

The __globals__ attribute shows the dictionary of names from the module.


Like, Comment, Share, and Subscribe

Did you find this helpful? Let me know by clicking the like button below. I'd love to hear your thoughts in the comments, too! If you want to see more content like this, don't forget to subscribe. Thanks for reading!


Mike Vincent is an American software engineer and app developer from Los Angeles, California. More about Mike Vincent