From 792f7a420a7a83843cf11fd2a743bd4445b7d990 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 15 May 2009 16:14:30 -0400 Subject: [PATCH] clarify first-class objects --- your-first-python-program.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/your-first-python-program.html b/your-first-python-program.html index 1b03456..b96bd04 100644 --- a/your-first-python-program.html +++ b/your-first-python-program.html @@ -216,7 +216,7 @@ SyntaxError: non-keyword arg after keyword arg

What’s An Object?

Everything in Python is an object, and everything can have attributes and methods. All functions have a built-in attribute __doc__, which returns the docstring defined in the function’s source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

Still, this doesn’t answer the more fundamental question: what is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser. Some objects have neither attributes nor methods, but they could. Not all objects are subclassable. But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function. -

You may have heard the term “first-class object” in other programming contexts. In Python, functions are first-class objects. You can pass a function as an argument to another function. Modules are objects. You can pass an entire module as an argument to a function. Classes are objects, and individual instances of a class are also objects. +

You may have heard the term “first-class object” in other programming contexts. In Python, functions are first-class objects. You can pass a function as an argument to another function. Modules are first-class objects. You can pass an entire module as an argument to a function. Classes are first-class objects, and individual instances of a class are also first-class objects.

This is important, so I’m going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Classes are objects. Class instances are objects. Even modules are objects.

Indenting Code

Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself.