folkerschamel,
@folkerschamel@mastodon.social avatar

for fans:
where does this error come from?


In my opinion, one of the great things about is that it's simple and straightforward - you don't get unexpected surprises. But there are a few exceptions, as this example shows.😉

JoeP,
@JoeP@mastodon.world avatar

@folkerschamel OK that is surprising and counter-intuitive, and not Pythonic.

This SO post https://stackoverflow.com/questions/38318370/what-are-list-comprehension-scoping-rules-within-a-python-class#38318470 referring to this in PEP 289 https://peps.python.org/pep-0289/#early-binding-versus-late-binding explains it ... and I don't like it at all!

TheWaterOnFire,

@folkerschamel

In class context like this, d is referred to as self.d

folkerschamel,
@folkerschamel@mastodon.social avatar

@TheWaterOnFire

A class scope has no "self", only methods have a parameter usually called "self" referring to an instance of the class.

TheWaterOnFire,

@folkerschamel

Yep, that’s right, responded too quickly.

Not sure what the practical value is of having this sort of logic at class scope, but yes.

folkerschamel,
@folkerschamel@mastodon.social avatar

@TheWaterOnFire

Constants, for example
STATES = ["a", "b"]
VALUES = ["x", "y"]
STATE_VALUE_COMBINATIONS = [s + v for s in STATES for v in VALUES]

cazabon,

@folkerschamel

The list comprehension changes the lookup namespace, so unqualified d doesn't exist.

This works:
>>> class Bar:
... c = [3, 4]
... d = [5, 6]
... e = [x + y for x in c for y in Bar.d]
...
>>> Bar.e
[8, 9, 9, 10]

Now whether that's a bug or not is another thing... :)

folkerschamel,
@folkerschamel@mastodon.social avatar

@cazabon You are cheating - only works if you had defined Bar already before 😉

cazabon,

@folkerschamel

Oops, so I am :)

It does look buggy to me, but I am really not the one to ask about the nitty-gritty details of the .

folkerschamel,
@folkerschamel@mastodon.social avatar

@cazabon

In fact, I'm not aware of any solution to this scoping problem (except for special cases where you can use zip or itertools.product).

Stark9837, (edited )
@Stark9837@techhub.social avatar

@folkerschamel

This is clear case for where zip should be used.

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel

But basically, if you put parentheses around the for loops you'll see that the code wouldn't run as expected.

folkerschamel,
@folkerschamel@mastodon.social avatar

@Stark9837 Not zip, but itertools.product.😉

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel

Oh wow! Don't know of that one. Thanks!

Itertools is a great built-in

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel

Just checked it out!

Is a weird one. Don't think I encountered any place or problem to use it except for mathematical operations, where I would normally just lean towards .

But I'll keep it in mind. I like functions like these, which force me to change my solutions to use optimized built-ins rather than my own solutions.

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel

Classic docs.

So naturally, as one does, after reading the product, I scrolled down and saw the repeat function. Now that is one I needed a few weeks ago!

Thanks for sending me down a rabit-hole!

@Python

djvdq,
@djvdq@mastodon.social avatar

@folkerschamel chained comprehension is scary 😅

In such things, I prefer to iterate over zipped lists

folkerschamel,
@folkerschamel@mastodon.social avatar

@djvdq
In this case it's not the same. The same would be itertools.product instead of zip.

djvdq,
@djvdq@mastodon.social avatar

@folkerschamel ah, right. Silly me.

folkerschamel,
@folkerschamel@mastodon.social avatar

@djvdq In any case, list comprehensions are fun, right?🙂

mrblissett,
@mrblissett@mastodon.social avatar

@djvdq
Mastopub needs a better way to share a code quote!

djvdq,
@djvdq@mastodon.social avatar

@mrblissett yep, Markdown support would be nice.

Stark9837,
@Stark9837@techhub.social avatar

@djvdq @folkerschamel

One thing I discovered is that zip is sometimes more useful for dictionaries than lists. The broad Itertools class always makes us think lists, sets, and tuples, but not dictionaries

@Python

folkerschamel,
@folkerschamel@mastodon.social avatar

@Stark9837 @djvdq @Python
For interest, what dictionary use case do you have in mind?

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

This is the first one off the top of my head. Sorry, I just got home from work.

https://gist.github.com/e-dreyer/19f0b5470b96d73a36180be4e24bb1d9

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

Lol, now I am on a roll! Here is another. For taking 3 dicts with the same structure and getting the separate values of matching keys

https://gist.github.com/e-dreyer/2e8a0c5d10522bd7860dff946f07ad0c

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

Last one😂 I promise!

Deep comparison of dicts using zip. This is possibly useless in higher level languages, but in something like C and C++, you would need this.

Well, also not. C++ allows for operator overloading, so you could overload == and in you could define eq, so it is one of those toy problems:

https://gist.github.com/e-dreyer/5cc62448cb58400399804d3b72756001

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

Final one, you just triggered my itch. Copying missing keys between dicts.

This one, for example, I used with an plugin:

https://gist.github.com/e-dreyer/eb421026e66ed4ad92140803df95dbfe

@Python

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

Most of these are nice. But once you hit a certain size, these functions become slow, and using hash maps and indexing is faster. So I would use this for less than 100 keys.

Most devs would lose it if you have a dict with 100 keys, but yeah. At that size, hashing is the same speed as the lookup or just a simple class if you uave the memory for it.

Most of the time I try to do stupid stuff like this rather than solving the actual problem in my own private projects.

@Python

folkerschamel,
@folkerschamel@mastodon.social avatar

@Stark9837 @djvdq @Python

I think we both are not the cool guys of the block, because I would do it in the same way as you and not use dictionary comprehensions 😉

Stark9837,
@Stark9837@techhub.social avatar

@folkerschamel @djvdq

Dictionary comprehension only changes the outer operators. This changes the inner logic an differentiates it from other logic. Thus, for me, my brain sees mistakes easier by keeping it separate from list comprehension.

List and dictionary comprehension is fun, but the built-in functions are implemented in C. Thus, they are faster and easier to spot mistakes.

So I use KISS for comprehension, anything more advanced I lean on built-ins.

@Python

JoeP,
@JoeP@mastodon.world avatar

@djvdq @folkerschamel But surely zip is a different result - of the same length as the original sequences (ie 2)? Rather than the "square" (ie 4) from the nested comprehension loops.

orsinium,
@orsinium@fosstodon.org avatar

@djvdq @folkerschamel You can use itertools.product

ljmc,

@folkerschamel I wouldn’t call chained comprehensions simple, and mostly advise against them, especially for this kind of scoping problem you’ve highlighted.

folkerschamel,
@folkerschamel@mastodon.social avatar

@ljmc

"I wouldn’t call chained comprehensions simple [...]"

Full agreement.

By the way, in my view chained comprehensions in work the wrong way. It should be [a for a in b for b in c], not [a for b in c for a in b].

erwinrossen,
@erwinrossen@mas.to avatar

@folkerschamel @ljmc I used to struggle with it too, but now I remember the ordering by mentally converting it too a multiline for-loop again, and keep the same order.

folkerschamel,
@folkerschamel@mastodon.social avatar

@erwinrossen @ljmc

Me the same!! We are soul mates! Interested in opening a bar together?

ljmc,

@folkerschamel I have to disagree with your view on ordering.

Outermost to innermost loop means new variable names appear left to right like in the rest of python.

Innermost to outermost would refer to variable names that have not been described yet and would require more jumping around.

1/2

folkerschamel,
@folkerschamel@mastodon.social avatar

@ljmc In [a for b in c for a in b], the variable a is also used before it was described yet.😉

ljmc,

@folkerschamel hehe yep, but that’s just how comprehensions work, however, no need to jump forward for b’s definition.

folkerschamel,
@folkerschamel@mastodon.social avatar

@ljmc
What about the English language?
Do you prefer this?

"I like the taste of all apples from all trees in all gardens of my neighborhood."

Or that?

"I like the taste of all apples in all gardens of my neighborhood from all trees."
😉

ljmc,

@folkerschamel I think English is not a very good programming language… but I do know what you mean. 😊

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • anitta
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • provamag3
  • tester
  • Leos
  • megavids
  • JUstTest
  • All magazines