!!! Objects and Names in Python

%%info
This is an additional exercise of the [MicroPython Tutorial|MicroPythonTutorial].
%%

!! 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:

%%info
What's going on? Below, we first enter the REPL, then declare a variable "x" whose value is "hello".
We then proceed to print out transformations of x's value using some of the methods that are available
to strings, like {{upper()}} (uppercase) or {{replace()}} (replace all instances of a pattern within
the string with a different value).

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)}}.
%%

%%repl {{{
/pyboard> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> 
>>> x = "hello"
>>> print(x)
hello
>>> print(x.upper())
HELLO
>>> print(x.capitalize())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'capitalize'
>>> print(x.replace("h","j"))
jello 
>>> print(x)
hello
>>>
}}} %%