Objects and Names in Python#
Objects#
In Python, everything is an object.
Formal Definition#
A name in Python is a reference (or binding) to an object. Names are created when you assign a value to a variable, define a function, class, module, or use import.
Less-Formal Definition (with a bit of explanation)#
A Python object is a piece of data stored in memory that has:
- identity (its memory address)
- type (what kind of object it is)
- value (its actual content)
This is true for:
- Numbers (int, float)
- Strings
- Lists, tuples, dictionaries
- Functions, classes, modules
- Even types themselves (type is an object)
In Python, a name is simply an identifier that refers to an object in memory. Think of it as a label or tag that points to a value.
Strings too!#
It may seem a bit strange, but in Python while strings have no attributes, they do have methods. Quite a few of them in fact, too many to list all of them here.
When you want to know what methods are available for a given type of object, you should consult the Python or MicroPython documentation. Because MicroPython is targeted for very small devices it doesn't support all the same features as the full Python version. In this case, the string method capitalize() is available in Python but not MicroPython, so the interpreter spits out an error message:
Note that executing these methods on the string do not alter its stored value, only return the result of the method being applied to the string. This is proved by the last print(x).