3
u/MegaIng Apr 06 '19 edited Apr 06 '19
I would actually disadvice the use of namedtuple in favor of dataclass (or at least NamedTuple). This is mainly for typehinting (which is a topic completely missing from the article.), but also for default parameter, mutability, and inheritance. Please, don't promot namedtuple.
0
u/CommonMisspellingBot Apr 06 '19
Hey, MegaIng, just a quick heads-up:
completly is actually spelled completely. You can remember it by ends with -ely.
Have a nice day!The parent commenter can reply with 'delete' to delete this comment.
2
u/BooCMB Apr 06 '19
Hey /u/CommonMisspellingBot, just a quick heads up:
Your spelling hints are really shitty because they're all essentially "remember the fucking spelling of the fucking word".And your fucking delete function doesn't work. You're useless.
Have a nice day!
2
0
Apr 06 '19
[deleted]
4
u/Airith Apr 06 '19
Exceptions in python are decently performant, it's quite common to use them for control flow. I don't think the user/age is a good example for them. Returning None would be akin to returning an error code like in C code.
1
u/random_cynic Apr 07 '19
More importantly I think patterns like 3 are commonly used as part of loops and instead of stopping whenever a particular name is not found it is much better to use some placeholder that indicates that the name was not found. The calling function should also be designed to handle different return conditions.
4
Apr 06 '19 edited Aug 28 '25
[removed] — view removed comment
1
u/random_cynic Apr 07 '19
That was the whole point of that post. It was showing the good way when you DO HAVE processing to do. It is not that rare for additional data processing when accessing attributes for example for logging purposes, tracing. For setting an attribute it is almost ALWAYS a good idea to implement an explicit setter and make sure that the attribute is set to an acceptable value.
-1
Apr 06 '19
[removed] — view removed comment
0
u/PiaFraus Apr 06 '19
When i see fruits, i know it's an iterable. The proper logic shouldn't give a damn of the types. All you know it's some sequence of something representing fruits. You can store them and print, and dont care about tyoes, together with this abomination of a syntaxis
0
u/PiaFraus Apr 06 '19
When i see fruits, i know it's an iterable. The proper logic shouldn't give a damn of the types. All you know it's some sequence of something representing fruits. You can store them and print, and dont care about tyoes, together with this abomination of a syntaxis
0
u/PiaFraus Apr 06 '19
When i see fruits, i know it's an iterable. The proper logic shouldn't give a damn of the types. All you know it's some sequence of something representing fruits. You can store them and print, and dont care about tyoes, together with this abomination of a syntaxis
1
Apr 07 '19 edited Aug 29 '25
[removed] — view removed comment
1
u/PiaFraus Apr 08 '19
Heh. The entire point of that example was that a Tuple does not have the same interface as a list.
Let's assume you are right and this is the point.
Now let's have the the name would be fruits_tuple. Now it's a tuple and has the same interface as a tuple. So condition in your "entire point" is satisfied.Did renaming resolve the antipattern?
No. Thus, the assumption that you are right and it was a point - is incorrect. The absolutely same set of problems with future developers would arise if your type hints are used and not enforced. "which can be confusing for other developers, or even your future self, who can act on the variable assuming that it is of the type mentioned in its name" - jsut change "in it's name" with in it's type hint.Hence, labeling the iterable "fruits_list" is therefore an "anti-pattern" (aka a bad idea).
Hence labeling the iterable (with type hint) fruits: Tuple[str, ...] is therefore an "anti-pattern" (aka a bad idea).
and when you ran mypy, it would have caught it. Or even better your interpreter could have caught it while you were still coding.
So now you are enforcing 3rd party tools?
But whatever floats your boat. Type hints are awesome and mandatory in my work's code base.
I am just glad I am not working with you.
P.S. I had a conversation regarding the type hint with a python core developer, whose videos are constantly praised in /r/python and he is not sold and had a list of good reasons, why type hinting is just making python looks more ugly, making readability worse without bringing much benefits.
Don't forget the "we are all consenting adults here". Python is not having private/protected fields in classes for the similar reason.
1
u/sneakpeekbot Apr 08 '19
Here's a sneak peek of /r/Python using the top posts of the year!
#1: The entire MIT Intro Computer Science class using Python is available for free, with course materials. | 83 comments
#2: These two books changed my life in 2018! | 181 comments
#3: xkcd: Python Environment | 400 commentsI'm a bot, beep boop | Downvote to remove | Contact ^^me | ^^Info | ^^Opt-out
3
u/groovitude Apr 06 '19
Good article.
I'll add that the "with" statement is not just for files; it's for anything with setup and teardown logic. You can build your own context managers with enter and exit methods.