[x-post from r/java] What Python concepts beyond the absolute basics should every developer have a excellent understanding of?

posted over here on /r/java.

So, what should they really know their way around?

15 comments · 72% upvoted · other discussions

comments

theywouldnotstand

Depends on how you define "absolute basics".

Some really useful things to know whether they fall under that umbrella or not:

  • All the builtins and how to use them
  • How to use special methods
  • String formatting
  • List and Dict comprehension expressions
    • The Dict section briefly describes an example of dictionary comprehension, it works very similarly to list comprehension so it doesn't merit its own section.
  • Argument and variable unpacking

```

unpack a list into a function accepting an arbitrary number of arguments

a = [1, 2, 3] some_function(*a) # same as some_function(1, 2, 3)

unpack a Dict into a function accepting arbitrary keyword arguments

kwa = {'a': 1, 'b': 2, 'c': 3} some_function(**kwa) # same as some_function(a=1, b=2, c=3)

unpack a list of items into separate variables

item_0, item_1 = [1, 2] item_0 # value is 1 item_1 # value is 2 ```

tilkau

Github triple-backtick code blocks aren't supported by reddit. Instead you need to prepend 4 spaces to each line in the code block. This would also fix a problem that looks like your comment lines are being parsed as headings.

The latter half of your list is also messed up, you probably need an additional newline to trigger list formatting (so the list looks like it is beginning a new paragraph, not 'touching' any other text). That happens to me a lot.

theywouldnotstand

Shows fine on the Reddit mobile app, fancy that.

And in fact, when I edit it to your suggestion, it is all messed up on the mobile app

Only place I see what you describe is the mobile version of the site in a browser. ¯\_(ツ)_/¯

[deleted]

[deleted]

lalligood

Although syntactically very similar to list/dict/set comprehensions, I would suggest including generators. Visually the only difference is that generators are framed in parens vs square brackets or curly brackets, but it's knowing when to use a generator vs a comprehension & why generators can be better when used properly (read: drastically reduced memory requirements).

theywouldnotstand

Don't forget using yield in a function to produce a more complex generator as well.

fxnut

Probably OT from what you’re after but wanted to point out that it’s fine and well worth taking the time to learn all the ins and outs of the language as shown elsewhere in the comments. But... if I’m hiring, the only thing I really care about is if you know how to make your code understandable at a (relatively) quick glance.

Related to this is, are you able to take something complex and implement it in code in a simple way?

Don’t be that guy/gal who: * Uses classes for everything and sets up so many subclasses you can’t follow the path of execution. * Likes to use list and dictionary comprehensions everywhere because they’re elegant structures but at the same time makes the code unreadable. Etc, etc...

It’s important to always keep in mind what coding is ultimately about: * Giving instructions to a computer for it to do something you want * Making sure you (or anyone else) can come back later and change and adapt those instructions easily

Some people are able to write Python with only the basic instructions and make it unintelligible. Whereas other people can use all the advanced techniques and syntax and make it easy to understand. Most times it’s all about consistency and clarity of thought and having a clear vision for the project.

theywouldnotstand

Making sure you (or anyone else) can come back later and change and adapt those instructions easily

Often, utilizing classes et al. is the way to make this happen, as it becomes reusable. DRY is a huge python mantra.

You are, however, correct, in that trying to be too clever leads to code that is unreadable by anyone but you. Use the right tool for the job within the time frame you have, and make sure, via comments and documentation or code clarity that it is easy for someone else to pick up and understand. That, however, is not a problem exclusive to python, it is a philosophy to take with you no matter what you're writing or what you're writing it with.

kumashiro

Ducks :) Although I don't think it is strictly a Python concept. Still, very important.

[deleted]

[removed]

kumashiro
WikiTextBot

Duck typing

Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine if an object can be used for a particular purpose. With normal typing, suitability is determined by an object's type. In duck typing, an object's suitability is determined by the presence of certain methods and properties, rather than the type of the object itself.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

dunkler_wanderer

Here's an interesting video that was posted a few days ago.

TL;DW

dunkler_wanderer

Here's an interesting video that was posted a few days ago.

TL;DW

[deleted]
Faal [OP]

I don't know, question is aimed to generalize important python terminology. Not so much to teach it or serve as a reference for learning.

officialgel

OOP is a game changer when adding to the basics of scripting.

[deleted]

go back to the shed, shitty bot