Dominoes posted:Objects are nice if you're defining something that can be thought of as a datatype, or if you're using a collection of similar items. I prefer to use namedtuples for this personally. E: which, I suppose, is actually a class, but people tend to treat them differently in their mental model of how things work.
|
|
# ¿ Mar 13, 2017 00:03 |
|
|
# ¿ May 5, 2024 09:17 |
ynohtna posted:Here's my common snippet to convert an iterable into natural sort order: It took me a little bit to prove to myself that you would never end up comparing a string and an int with this, but yeah, you never do so this is totally valid. Nice!
|
|
# ¿ Apr 26, 2017 04:57 |
huhu posted:What would be the best way to take: I'd do Python code:
Edit: the difference between Thermopyle's solution and mine is that if there are other items in the dictionaries, his will select out the values with 'amt' keys, whereas mine will just give all the values regardless of key. VikingofRock fucked around with this message at 15:51 on Jun 14, 2017 |
|
# ¿ Jun 14, 2017 15:48 |
So I've been doing more python lately, and while I feel like I have a decent grasp on the language itself, I struggle with a lot of the idioms for structuring a program. For example, how should I layout the directory structure of my program, what is __init__.py, where do I list my dependencies, stuff like that. Does anyone have a good resource for learning that sort of stuff? Like the things that are necessary to use python effectively, but which are outside of the scope of the language itself. edit: I often don't really know what various tools are, either, which falls into the same category. Like what is virtualenv / venv / pyvenv, etc? Is there a standard python code formatter? What other tools should I know about? VikingofRock fucked around with this message at 03:24 on Jun 23, 2017 |
|
# ¿ Jun 23, 2017 03:21 |
accipter posted:Here is the official documentation: https://docs.python.org/3/tutorial/modules.html#packages , and this is pretty good guide on how to package a Python module: https://python-packaging.readthedocs.io/en/latest/index.html . I would also recommend the setuptools documentation: https://setuptools.readthedocs.io/en/latest/index.html . However, it is a more focused on setuptools, rather than the organization of a package. How to organize a package depends on the scale of the package. If it is small enough, you can put everything in __init__.py. Thank you for all the information, this is very helpful. I'm worried that there are more questions like this that I don't even know to ask, though. Is there a book or something that covers these topics for python? Edit: I'm looking at Effective Python. Does anyone have any experience with that book? If so, would you recommend it? VikingofRock fucked around with this message at 03:59 on Jun 24, 2017 |
|
# ¿ Jun 24, 2017 02:58 |
Thermopyle posted:Say i have a routine that searches for and gets a thing. You can search for a thing via a regex, via an int, or via a tuple. Which of the following do you prefer? Python code:
where predicate is a function/callable which takes as input elements of the space you are searching over and which returns true if a match is found. If you don't want to do that though, I think the first function you posted has the cleanest interface so I would prefer that.
|
|
# ¿ Aug 5, 2017 19:28 |
Thermopyle posted:I don't know why I didn't even think of using a predicate since most of my time is spent doing JS nowadays and thats a very common pattern there. Thanks for reminding me. Nice, that sounds like an excellent, convenient interface.
|
|
# ¿ Aug 6, 2017 06:19 |
Why does this happen? (Python 3.6.3)Python code:
|
|
# ¿ Dec 26, 2017 23:22 |
VikingofRock posted:Why does this happen? (Python 3.6.3) Okay, so I asked a friend and he said that it's because the lambda is storing a reference to val, and so when val is updated throughout the dictionary comprehension it changes the val "pointed to" by all the lambdas. And this works: Python code:
|
|
# ¿ Dec 27, 2017 00:02 |
|
|
# ¿ May 5, 2024 09:17 |
QuarkJets posted:Is there a reason that you're returning a lambda instead of just x/val? This question actually came from me helping someone else with some python (and getting stumped), but presumably the answer is "because you don't know x at the time when this dictionary is created".
|
|
# ¿ Dec 27, 2017 02:17 |