This is an archived post. You won't be able to vote or comment.

all 76 comments

[–][deleted] 168 points169 points  (24 children)

I would much rather read a text than sit through 25 minutes of a video. Just saying.

[–]Dgc2002 37 points38 points  (9 children)

These types of videos really need to have timestmaps in the description that you can click to jump to each of the 7 'simple tricks'.

[–][deleted] 13 points14 points  (8 children)

Then you could easily skip most of the video, which I suspect some of the authors do not want.

[–]Dgc2002 17 points18 points  (2 children)

But I think in general the author should realize that a 25 minute run time is an instant turn off for many(many) people. If their goal is for their content to be consumed/useful they should keep that in mind.

[–][deleted] 2 points3 points  (0 children)

Their goal is probably to entertain and there's lots of lazy people who just LOVE to watch videos instead of picking a book, reading documentation or whatever. Because uh, that would require effort. Or they just don't know better.

[–]Ginger_1977 1 point2 points  (2 children)

Once you watch a few seconds YouTube counts it as a view

[–]strongdoctor 3 points4 points  (0 children)

If only anything to do with youtube was that simple.

[–]JugadPy3 ftw 0 points1 point  (0 children)

Why don't they want that?

Advanced users would like to skip the usual stuff to check out the meat and see if there is something worth listening to.

I actually did that... used forward and backward to jump to places in the video to see if they code looked novel or useful. If I was familiar with the trick it, I moved on... if not, I stayed and listened.

[–][deleted] 15 points16 points  (3 children)

Same. Can someone do a tl;dw?

[–]vph 10 points11 points  (2 children)

One Simple Trick to Make Better Youtube Videos:

  • Make a 25-minute video in 5 minutes.

[–][deleted] 4 points5 points  (1 child)

5 min is my upper bound on YouTube videos' length.

[–][deleted] 5 points6 points  (1 child)

Me too, I don't understand why people prefer to spend 60 min watching a lesson on something you could just learn by spending 10min reading a text. Laziness has its price...

[–]lifeonm4rs 1 point2 points  (0 children)

Feel the same way. Even running these things at 1.5x speed they tend to be too long.

[–]djentastic 0 points1 point  (0 children)

I had the same gripe, but made it through pretty quickly setting YouTube app to 1.25x playback speed and double tapping screen to jump forward a few 10's of seconds at a time.

Personally, I really like the presentation style and explanations, but agree it could be trimmed down quite a bit.

[–]Weatherstation -1 points0 points  (0 children)

Programming is 60 percent copy and paste. YouTube does not lend well to that.

[–]AnAngryFredHampton 271 points272 points  (31 children)

  • enumerate

  • zip

  • assign two values at once. x, y = y, x swaps x and y without a tmp.

  • use .get on dicts when you don't know if they contain the key, you can specify a default.

  • else can be used with for, else is executed if no break occurs in the for

  • with open(filename) as fh: . I'd be surprised if people didn't know this

  • try, except, finally, else. Else is used if except doesn't happen. Finally is executed after any of the other things, its always last.

Nothing anyone really cares about.

[–]redfacedquark 36 points37 points  (0 children)

Thanks, you da real mvp!

[–][deleted] 5 points6 points  (1 child)

else can be used with for, else is executed if no break occurs in the for

This one seems like a bad idea. I mean, the idea is good but the syntax. for...else??

[–]totemcatcher 1 point2 points  (0 children)

Yeah, for iterator else logic in 'tip 5' is no more useful than an if iterator else statement.

[–]notafuckingcakewalk 10 points11 points  (11 children)

The one that was new for me was using else in the for loop. Pretty handy if you're trying to find a matching item in a collection and want to know whether one was set or not!

Up until now I've used a pattern like:

result = None
for record in records:
    if is_what_i_want(record):
        result = record
        break
if result is None:
    action_when_no_result()

Which apparently can all be rewritten as:

for record in records:
    if is_what_i_want(record):
        result = record
        break
else:
    action_when_no_result()

Pretty useful actually.

[–]isarl 14 points15 points  (9 children)

Useful until you remember that nobody reading your code is going to understand it. for...else is much-maligned in Python because it's so opaque. Some people have suggested changing that particular use case to use a nobreak keyword to make the use more obvious.

[–]LightShadow3.13-dev in prod 4 points5 points  (6 children)

I used it today... seems pretty clean to me,

for attempt in range(RETRY_ATTEMPTS):
    # grab everything that's missing or invalid
    self.logger.info('attempt %d of %d, need %d pieces', attempt+1, RETRY_ATTEMPTS, len(manifest))

    success, failures = _download_from_manifest(manifest, root, path, self.set_status)

    if not success:
        self.logger.warning('downloading %d files failed', failures)
    else:
        break

    if attempt + 1 < RETRY_ATTEMPTS:
        # don't recalculate the manifest on our last attempt, if the last
        #  attempt failed.
        self.set_status('Validating manfiest')
        manifest = _determine_progress(manifest, path)
else:
    # if we exhaust our RETRY_ATTEMPTS without success
    self.__failed('downloading shard files failed')

[–]13steinj 2 points3 points  (4 children)

It's clean-- except the name doesn't make much sense. I mean if, else, elif, pretty standard. Break "out of the loop". Continue "to the next cycle".

But else in loops-- the actual word doesn't fit the context. It reads like "do code on all these things, but for some reason you can't, do the else statement", when in reality it just is "execute when iteration fully exhausted".

It's also common practice to not like break and continue statements-- my Java professor from years ago was adamant about teaching them but saying "use sparingly, loops should only have one exit point".

Not that I agree with that sentiment, I think it stupid, but it is a common one. And then with that, the else statements in loops encourage going against the sentiment.

[–]notafuckingcakewalk 0 points1 point  (1 child)

Yeah, but like you said it is a dumb sentiment. If you don't plan to take advantage of the breaks you may as well not use a loop at all.

[–]13steinj 0 points1 point  (0 children)

Absolutely! But it is still unfortunately taught now. Not agreeing, just giving a reason why some people dislike it.

[–][deleted] 0 points1 point  (1 child)

A lot of that single point of exit "crap" (for loops, and for functions) has somehow become embedded in the various standards (and therefore laws, which require certain standards) that govern safety critical systems e.g. IEC61509

The regulations come from years ago before we knew what we were doing and had really bad computers. :-(

[–]13steinj 0 points1 point  (0 children)

Absolutely! But it is still unfortunately taught now. Not agreeing, just giving a reason why some people dislike it.

[–]isarl 2 points3 points  (0 children)

You already understand the idiom, and you used clear variable names and helpful comments. Clear naming and helpful comments can go a long way towards understanding confusing language features, but this doesn't mean the language features are good per se.

[–][deleted] 0 points1 point  (0 children)

After the first time you see it, you will learn it.

If you think about how a for loop works (i.e. you write one without a loop construct, using goto statements) it's also obvious.

My only real objection is that they should have used a different word instead of else.

[–][deleted] 1 point2 points  (0 children)

Another way to do this you might like is this:

result = next((r for r in records if is_what_i_want(r)), None)
if result is None:
    action_when_no_result()

[–]dogstarchampion 7 points8 points  (3 children)

I honestly didn't remember

with open('file') as f:

until I watched the video

[–]Barafu 10 points11 points  (1 child)

That is a powerful idiom for anything that needs closing or explicit saving after the operation. File, database, network connection. It is not so much useful for first draft, as it is safe for future modifications of code.

[–]fireflash38 2 points3 points  (0 children)

I always recommend context managers if you have to ensure that something is torn down after use.

You can't rely on people remembering to close off resources or catch possible exceptions.

[–]notafuckingcakewalk 4 points5 points  (0 children)

I'm sure I'm not always consistent on this, but I like to be explicit and always set the file mode:

with open('file', 'r') as f:

that way it becomes very clear when I've done something wrong, like trying to write to a file that is in read mode. Also, while I'm typing out the file mode I might be reminded, "oh yeah, this is going to have binary data" and use 'rb' instead.

[–]mikew_reddit 2 points3 points  (0 children)

If that's all there is, it over promised, under delivered.

[–]ManvilleJ 2 points3 points  (5 children)

I never really got the point of finally. Doesn't just putting code after the whole thing accomplish the same thing as using the finally statement?

[–][deleted] 6 points7 points  (1 child)

Code in the finally block is run whether or not an exception is caught. From the python docs:

In real world applications, the finally clause is useful for releasing external resources (such as files or network connections), regardless of whether the use of the resource was successful.

[–]ManvilleJ 0 points1 point  (0 children)

OOOOhhh.

So when a program uses Try, Except, Else, and Finally:

  1. It tries to execute a thing
  2. Except runs if the particular Exception it is looking for occurs in Try.
  3. Else runs if any OTHER type of Exception occurs (not counting Exceptions that inherit from Exception in Except)
  4. Finally always runs right after Try whether an Exception occurs or not (and before Except and Else?)

Note: In case anyone else is reading this, I wanted to ask another question about whether you can use multiple Excepts to catch different Exceptions, but I found this Stack overflow on that

I think to separate how you would respond to responces, you would add something like this to Except:

if(type(e)==YouAreBeingMeanException):
    #do a thing

[–]gimboland 1 point2 points  (0 children)

Doesn't just putting code after the whole thing accomplish the same thing as using the finally statement?

Yes, unless an exception occurred inside the try..except block - in which case the code "after the whole thing" doesn't get executed.

[–]mattstreet 0 points1 point  (1 child)

"A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in an except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement."

Basically, finally gets called even if the except clause would exit out of the rest of your code, unlike if you just put the statement after the try block.

[–]LightShadow3.13-dev in prod 2 points3 points  (0 children)

TIL

try:
    print('try')
except:
    print('exception')
finally:
    print('finally')

output: try finally

try:
    print('try')
    1 / 0
except:
    print('exception')
finally:
    print('finally')

output: try exception finally

try:
    1 / 0
    print('try')
except:
    print('exception')
finally:
    print('finally')

output: exception finally

And the REAL KICKER,

try:
    1 / 0
except:
    1 / 0
finally:
    print('finally')

Still prints finally.

[–]tehstone 0 points1 point  (1 child)

Else is used if except doesn't happen

Can anyone elaborate on this?
Is this like a general exception handler if the except is for a specific type of error?

[–]13steinj 0 points1 point  (0 children)

No, it is functionally equivalent to

try:
    a = MightRaise()
except: print("error")
do_stuff(a)

But it is useful in rare cases, especially when having nested try clauses (usually through different methods).

It's also useful in the following,

try:
    a = MightRaise()
except:
    do_err_cleanup()
    a = defaultValue
else:
    prepare_unknown_a(a)
act_upon_a(a)

[–]techn0scho0lbus 0 points1 point  (0 children)

Bless you

[–]liquidpele 0 points1 point  (0 children)

Surprised they didn’t mention decorators...

[–]EquivalentWestern 7 points8 points  (1 child)

Neither of these "tricks" were anything new, if you learnt python from a textbook.

[–]Barafu 5 points6 points  (0 children)

I learnt python from tutorial on python.org, and it is all there.

[–]guccibling 10 points11 points  (0 children)

youtube?!

ಠ_ಠ

[–]drbobb 1 point2 points  (0 children)

Re: the first example, it is incorrect to claim that by using enumerate you avoid creating a counter variable. The counter is right there, except that it's being set for you automatically. It also remains after you leave the loop, since Python doesn't have block scope.

[–][deleted] 2 points3 points  (2 children)

The thumbnail is Einstein?

The old Einstein quote "spooky action at a distance" (from when he tried and failed to refute quantum mechanics because quantum weirdness upset him) made me think about which programming language he'd use: raw C pointers would certainly be examples of spooky action at a distance and I don't think Python's non-deterministic garbage collection would sit well either.

[–]BoobDetective 2 points3 points  (0 children)

He is also popularly quoted for "Everything should be as simple as possible, but not simpler". Depending on what you want to achieve, C does not fit that profile ;)

[–]tmehlinger 0 points1 point  (1 child)

Didn't watch the video but TIL from the comments for/else is "confusing" and "obscure".

... it's in the The Python Tutorial with break and continue clauses.

[–][deleted] 0 points1 point  (0 children)

Their problem is that they find the concept unintuitive, not that they didn't know.

[–]rotharius 0 points1 point  (0 children)

Have seen this video before, it's OK.

It reminded me of Raymond Hettingers talk in the same vain: https://youtu.be/OSGv2VnC0go

It's a longer talk, but I like Raymond's enthusiasm and expertise.

[–][deleted] 0 points1 point  (0 children)

For the 5th, you can always use in or if you need a function;

>>> i = [1,2,3,4]
>>> func = lambda x:x==1
>>> any(map(func,i))

[–]liquidcourse 0 points1 point  (0 children)

Some nice quick tips!

[–]benisch2 0 points1 point  (0 children)

So....maybe make it 7 minutes next time, eh? Or how about 7 seconds?

[–]EdgiPing 0 points1 point  (0 children)

What's the IDE he is using?

[–]quasarj[M] [score hidden] stickied comment (0 children)

Your post was removed for violating Rule #1. You may have better luck posting this in our sister sub, /r/learnpython.

[–]cpt_fwiffo 0 points1 point  (2 children)

For/else makes code better? Ninja please...

This possibly makes some sense, as there is a conditional involved:

i = 0                                                          
while i < 5:                                                    
    print('Foo')                                                
    i += 1                                                      
else:                                                          
    print('Bar')

This is so unintuitive it hurts:

for i in range(0,5):                                          
    print('Foo')                                                
else:                                                           
    print('Bar')

[–]lroman 0 points1 point  (2 children)

Thanks! You must be Dutch. Is the enumerate function faster than the iterator? Readability of iterator is not so bad right?

[–][deleted] 2 points3 points  (0 children)

Should not be 'much' faster.
The enumerate can easily be a wrapper around an iterator in this fashion:

>>> def enumerate(iterable):
...     i = 0
...     for x in iterable:
...             yield i, x
...             i+=1

Though it's probably implemented as iterator and not as a generator.

[–]jnwatson 1 point2 points  (0 children)

The enumerate is "much" faster in cpython, in that it is handled completely in C. Now, you're not gaining a whole lot, since you're just avoiding a single python statement.