-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dot notation question #10
Comments
It seems that the following for todo in todo_list:
if todo["id"] == id:
todo[".item"] = todo_data # relace with the request body
return { "message": "To-do updated successfully" } Fails with the |
Hi, @badlydrawnrob! I'm glad you're enjoying the book so far. For the todos, we're making use of Pydantic Basemodel to represent the schema hence the use of dot notation to access the fields. class Todo(BaseModel):
id: int
item: str For the PUT method you have, the error is as a result of the wrong way you're accessing the Let me know if this answers your question. |
@Youngestdev Thanks for the speedy reply. I think I understand now, so basically the from pydantic import BaseModel
class ToDo(BaseModel):
id: int
item: str
# json
one = { "id": 1, "item": "Finish the chapter" }
# our model
todo = ToDo(**one) And then you're accessing the attributes with dot notation:
I'm so used to using typed functional (Haskell) I've totally forgot how object-oriented languages work 🤦♂ |
@Youngestdev Hi there, I'm working my way through your book and enjoying it so far, but I have a simple question about the syntax (it's been a long time since I've used Python) ... by default
item = {"id": 1}
dictionary can only be accessed usingitem["id"]
syntax, yet the examples in the book use dot notation (item.id
).I'm assuming our data in chapter 02 is using the
todo_list
(a list ofToDo
dictionaries) and converting requests fromjson
todict
(which we're accessing).I can't seem to find this information anywhere.
The text was updated successfully, but these errors were encountered: