In this lesson, you’ll see some of the problems you could run into if you use mutable data structures to store your data set. Then, you’ll see how you can approach this data set with immutable data sctrutures.
You’ll learn how you can use .namedtuple()
from the collections
module, which is built into Python, in order to represent your data in an immutable data structure so it can’t be modified in-place.
Which editor is being used in the video?
Which IDE/Command prompt have you used.Can you please tell me.It looks good.
I’m using an alternative Python REPL called bpython
in my videos. You can learn more about it here: bpython-interpreter.org
Hi @Dan Bader, just wanna correct your article above collection.namedtulple
to collection.namedtuple
.
Thanks for catching my typo there, should be fixed now :)
is it possible to have a pdf of these wonderful lessons?
Hey, in the namedtuple function, what does the first index, scienctist represent
@Han It is the typename. collections.namedtuple returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable.
hey i think we can change names of scientists using ada._replace(name=”xxx”) function so the named tuple is also mutable am i right once check the functionality of _replace function in namedtuple
hey dan can you please respond to my commment
@pavanala13433
_replace()
creates a new named tuple with certain values replaced. It does not mutate the original named tuple:
>>> from collections import namedtuple
>>> Point = namedtuple("Point", ["x", "y"])
>>> p = Point(1, 2)
>>> p
Point(x=1, y=2)
>>> p._replace(x=3)
Point(x=3, y=2)
>>> p
Point(x=1, y=2)
Thank you very much sir @Geir Arne Hjelle for responding and cleared my doubt with your explanation.....
Began learning how to code this spring and this course is a bless! I do like Dan’s teaching quite much as well (His courses on YouTube that made me a paying member on Real Python).
However. Since start, I haven’t liked OOP for some reason… Since I grasped the concept somewhat - in my case barely removed the dust from the box - I’ve all the time the feeling of “passing the river to collect water”. Guess I don’t understand the concept enough.
Gah, now I’m loosing the thread. My intension is NOT going into any OOP debate (understand it’s a thing). What do I know. I just wanted to point out this style of coding appeals more to me =)
Thanks for your comment Jakob, glad to hear that :) The way I look at FP vs OOP is that they’re just different “tools in the tool box” and I try to pick whichever I find the most appropriate given the circumstances. And subjective appeal/enjoyment can be a great reason for choosing one style over another! (At least on hobby projects or projects that don’t have to stick to an already established architectural style.)
By the way, if you’re looking for resources to make Python OOP “click” then check out our Python OOP Learning Path here on Real Python!
Thx for the link to RP’s OOP Learning Path. I will grind the whole thing during upcoming weeks. Btw, it must be written:
“Give me all the x:es where x is in scientists but only if x.nobel is True”. The way you commented between syntaxes while writing this finally made me confident with list comprehensions.
It’s fascinating how effective tiny details sometimes are =)
Become a Member to join the conversation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4