
Ejercicio sobre diccionarios en Python
Un ejercicio difícil para construir un modelo mental correcto de los datos en Python.
El enlace «Solución» utiliza memory_graph para visualizar la ejecución y mostrar lo que ocurre realmente.

Un ejercicio difícil para construir un modelo mental correcto de los datos en Python.
El enlace «Solución» utiliza memory_graph para visualizar la ejecución y mostrar lo que ocurre realmente.
❓ Your functions work, but now you want to add logging to them. Do you edit each function?
🎯 A decorator makes this easier: wrap a function and add logic before and after the original call.
⚙️ In this decorator example we create and use our '@log_call' to log each call with arguments and return value.
🔎 Here 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵 shows how a decorator gets evaluated step by step for better understanding.
Useful decorators from Python’s standard library include:
Other common custom decorators add: timing, retries, conversion, authentication, validation, ...
Este clásico ejercicio de programación para principiantes utiliza un algoritmo voraz. Pero lo interesante es que 𝐦𝐞𝐦𝐨𝐫𝐲_𝐠𝐫𝐚𝐩𝐡 nos permite ver qué ocurre en la memoria mientras se ejecuta el código.
Visualiza paso a paso el estado del programa: variables, valores, listas, diccionarios y referencias. Esto ayuda a los estudiantes a comprender y depurar su código.
Ejecuta esta demo interactiva.
¿Qué otros ejercicios de programación se beneficiarían de una visualización con memory_graph?
Consulta más visualizaciones de memory_graph.
An exercise to help build the right mental model for Python data.
The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.
An exercise to help build the right mental model for Python data.
The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.
A binary tree can be traversed in three classic ways. The difference is simply the order in which you visit the subtrees:
This interactive memory_graph example visualizes each traversal method step by step: Binary Tree Traversal
Also see other memory_graph visualizations.
No post da semana passada, mostramos que um laço for em Python funciona por meio do Protocolo de Iteração.
Esse mesmo protocolo também permite que valores sejam produzidos de forma preguiçosa.
Neste novo Exemplo de Avaliação Preguiçosa, mostramos um consumidor lendo valores de cinco fontes diferentes:
O mesmo laço for consome as cinco fontes. As fontes antecipadas produzem todos os valores antes que o consumidor comece a processá-los. Nas fontes preguiçosas, o produtor e o consumidor se alternam:
Cada valor é produzido somente quando o laço for o solicita.
Funções geradoras e expressões geradoras são maneiras concisas e legíveis de criar iteráveis preguiçosos. A última fonte torna explícito o mecanismo subjacente:
iter() obtém um iterador.next() solicita o próximo valor.StopIteration indica que não restam valores.Isso mostra claramente que os geradores realizam a iteração preguiçosa implementando o Protocolo de Iteração do Python.
In last week’s post we showed that a Python for-loop works through the Iterator Protocol.
This same protocol also enables values to be produced lazily.
In this new Lazy Evalution Example we show one sink reading values from five different sources:
The same for-loop consumes all five sources. The eager sources produce all values before the sink starts consuming them. In the lazy sources producer and consumer take turns:
where each value is produced only when the for-loop requests it.
Generator functions and generator expressions are concise, readable ways to create lazy iterables. The final source makes their underlying mechanism explicit:
iter() obtains an iterator.next() requests the next value.StopIteration signals that no values remain.This clearly shows that generators achieve lazy iteration by implementing Python’s Iterator Protocol.
An exercise to help build the right mental model for Python data.
The “Solution” link uses package memory_graph to visualize execution and reveals what’s actually happening.
What actually happens when Python executes a for-loop?
for value in container:
print(value)
Behind the scenes, Python uses the iterator protocol:
iterator = iter(container)
while True:
try:
value = next(iterator)
print(value)
except StopIteration:
break
iter(container): creates an iterator.next(iterator): retrieves one value at a time.When there are no more values, the iterator raises StopIteration. The for-loop catches this exception automatically and ends the loop.
For containers that support backward iteration, Python also provides:
reversed(container): creates a backward iterator.We can support these operations in our own classes by implementing:
__iter__()
__reversed__()
__next__()
This provides a powerful abstraction: an algorithm can process values without needing to know how a container stores them internally. The same algorithm can therefore work with lists, sets, dictionaries, linked lists, trees, and many other containers.
Here's an example that uses 𝐦𝐞𝐦𝐨𝐫𝐲_𝐠𝐫𝐚𝐩𝐡 to show the use of iterators on a Linked_List making the invisible mechanics of iteration visible for easy understanding.
Tony added the Cupta unit, a serpent of dark magic who returned from the caverns and tries to siphon your life essence, with this git commit.
Tony also changed the control of player's direction from keyboard to mouse, making the player easier to control. But, this now only works when the mouse pointer is inside the game window. If it's outside the direction is still controlled by keyboard as before. So you can now choose the direction control that you prefer.
See the PythonLearningGame repo to add your own game elements to this collaborative project, or simply tell us what you would like to see added to the game.
Anonymous-Me-Me-Me added a new Putler unit with this git commit and message:
Putler unit added, any likeness to real people is completely coincidental. It wants you to sign its contract, but if you get too close it tries to run to its bunker. Best to zero it before it goes into history for destroying an "empire".
We did had to add sunglasses for copyright and identity protection reasons.
PythonLearningGame is a collaborative Python project for fun and educational purposes. To add your own game elements see the PythonLearningGame repo.
An exercise to help build the right mental model for Python data.
# Output of this Python Program?
def main():
o1, o2 = MyClass(1), MyClass(1)
myset = {o1}
print(o2 in myset, end=' ')
o1.set_value(1000)
print(o1 in myset, end=' ')
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
main()
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
def __eq__(self, other):
return self.v == other.v
def __hash__(self):
return hash(self.v)
main()
# --- possible answers ---
# A) TypeError: unhashable type: 'MyClass'
# B) True False False False
# C) True False False True
# E) False True True True
# D) False True True False
# See "Solution" for correct answer.
__eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."Um exercício para ajudar a construir o modelo mental correto sobre os dados em Python.
An exercise to help build the right mental model for Python data.
# Output of this Python Program?
def main():
o1, o2 = MyClass(1), MyClass(1)
myset = {o1}
print(o2 in myset, end=' ')
o1.set_value(1000)
print(o1 in myset, end=' ')
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
main()
class MyClass:
def __init__(self, v):
self.v = v
def set_value(self, v):
self.v = v
def __eq__(self, other):
return self.v == other.v
def __hash__(self):
return hash(self.v)
main()
# --- possible answers ---
# A) TypeError: unhashable type: 'MyClass'
# B) True False False False
# C) True False False True
# E) False True True True
# D) False True True False
# See "Solution" for correct answer.
__eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."An exercise to help build the right mental model for Python data.
__eq__() and __hash__() methods by default (inherited from the object class); with them, all objects compare unequal (except with themselves) and x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y)."A Battle Tail is added with this git commit.
PythonLearningGame is a collaborative Python project using PyGame, just for fun and purely educational. We are looking for collaborators.
Surely some of you think you are pretty good with Python, then this is your chance to show us what you can really do besides writing know-it-all comments. Maybe add:
See the PythonLearningGame repo for instructions.
An exercise to help build the right mental model for Python data.
The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening.
With this git commit the player got a direction to move and shoot in making our collaborative game more interesting.
To play this game and make further additions to it, see the PythonLearningGame repo.
Who can add Health Pickups to top up the player's hitpoints? Maybe heart shaped?
Let us know what other game elements you would like to see.
Programming courses often focus heavily on understanding code, while paying far less attention to understanding the program state. But code does not exist in isolation. Its main goal is to change the program state, before ultimately producing some output.
To develop an accurate mental model of program execution, students need to understand both:
Reading code alone does not always reveal how the program state changes during execution. That is why I created 𝗺𝗲𝗺𝗼𝗿𝘆_𝗴𝗿𝗮𝗽𝗵: a tool that visualizes the state of a Python program as it changes, step by step.
It can help explain a wide range of introductory Python topics. Here are just a few examples:
Instead of reconstructing the program state from print statements, students can now watch it change as each line executes. This makes unfamiliar concepts easier to understand and bugs easier to fix.
Help your students learn Python programming more thoroughly and easily.
See: more examples