Python's comma equals ,= operator?

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ต.ค. 2020
  • Do you know what x ,= y does in Python?
    ― mCoding with James Murphy (mcoding.io)
    Source code: github.com/mCodingLLC/VideosS...
    SUPPORT ME ⭐
    ---------------------------------------------------
    Patreon: / mcoding
    Paypal: www.paypal.com/donate/?hosted...
    Other donations: mcoding.io/donate
    BE ACTIVE IN MY COMMUNITY 😄
    ---------------------------------------------------
    Discord: / discord
    Github: github.com/mCodingLLC/
    Reddit: / mcoding
    Facebook: / james.mcoding
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 430

  • @prinsniels
    @prinsniels 3 ปีที่แล้ว +922

    It's true use case is to check if people actually read your pull request before approving it.

    • @mCoding
      @mCoding  3 ปีที่แล้ว +165

      It is also commonly used in matplotlib plotting code (unfortunately). E.g.
      line, = ax.plot(df, label=label)
      I mess this up EVERY time.

    • @Hermaniac8
      @Hermaniac8 3 ปีที่แล้ว +50

      @@mCoding I encountered exactly that usage when I was trying to learn how to use matplotlib. I couldn't understand it at first, and I found it to be a very hard question to ask Google, because it's hard to put into words what you're asking about.

    • @toxic_narcissist
      @toxic_narcissist 3 ปีที่แล้ว +23

      @@Hermaniac8 it's not a hard question to Google, though
      "python comma equal"
      "python comma before equal"

    • @atussentinel
      @atussentinel 3 ปีที่แล้ว +23

      ​@@mCoding Anyway line, = ax.plot(...) is considered bad practice. I tended to use line = ax.plot(...)[0]. Not only the comma is tiny to spot, also plot() can return multiple lines if y is 2-d array, in such case line, = ax.plot(...) will result in an error.

    • @georgplaz
      @georgplaz 3 ปีที่แล้ว +9

      @@toxic_narcissist often with these problem, its not enough to just google what you see, because you don't know which different forms this thing you don't understand can take on.
      now with the hindsight of what it actually is and that it always looks like this (comma with missing element afterwards and an equal sign), its easy to put into words

  • @zeawoas
    @zeawoas 3 ปีที่แล้ว +415

    so it's just pattern matching and there is no actual `,=` operator? I feel betrayed!

    • @mCoding
      @mCoding  3 ปีที่แล้ว +107

      And I feel betrayed everytime matplotlib uses it!

    • @CrapE_DM
      @CrapE_DM 3 ปีที่แล้ว +4

      Not technically pattern matching, but close enough

    • @rmaster934
      @rmaster934 3 ปีที่แล้ว +14

      --> operator in C/C++

    • @jlee3361
      @jlee3361 3 ปีที่แล้ว +4

      @@mCoding FREAKING MATPLOTLIB LOVES THIS. I had a mini breakdown when I first saw this and had no idea what it was

    • @RGAM1989
      @RGAM1989 2 ปีที่แล้ว +5

      @@jlee3361 Shouldn't Matplotlib avoid such "syntax" in its implementation and documentation? Do them, at least, separate the , and the =?

  • @0LoneTech
    @0LoneTech 3 ปีที่แล้ว +347

    Certainly. It may be clearer if you spell it "(x,) = y". It is a deconstructing assignment, extracting the element of a single item container. The more common use is things like "x,y = 2,3" or "a,b = b,a".

    • @ianliu88
      @ianliu88 3 ปีที่แล้ว +39

      or you could also make it generic to take the head by writing x, *_ = y :P

    • @robmckiernan3264
      @robmckiernan3264 3 ปีที่แล้ว +14

      @@ianliu88 This is the most readable

    • @0LoneTech
      @0LoneTech 3 ปีที่แล้ว +1

      @@jonathanflatley793 Maybe if the formatter is exceptionally bad at its job; it should not have let ",=" exist in the first place. autopep8 for instance leaves the parenthesis but changes "x,=y" to "x, = y" (per the rule to put spaces around the = in an assignment statement, which applies to both cases).

    • @AnoopVargheese
      @AnoopVargheese 3 ปีที่แล้ว

      to be more precise, it's a special case of of python's Tuple Unpacking syntax. It's a lot more powerful than just swapping values.

    • @stevenluoma1268
      @stevenluoma1268 ปีที่แล้ว

      I definitely went "that sure looks like I'm tuple unpacking, I'm gunna guess that it's 2!" I'm a noob though.

  • @Ezechielpitau
    @Ezechielpitau 3 ปีที่แล้ว +134

    I don't know. Kinda interesting but
    x = primes.intersection(evens).pop()
    is still more readable. Also doesn't crash if I change something and now have a set containing more than one item

    • @mattmess1221
      @mattmess1221 3 ปีที่แล้ว +21

      x, *rest = hello()

    •  3 ปีที่แล้ว +29

      In most cases you would want the program to crash (raise an exception) when the set has different number of elements than you expect.

    • @deViant14
      @deViant14 3 ปีที่แล้ว +5

      @ if you always knew why wouldn't you just hard code the resulting set

    • @nallid7357
      @nallid7357 3 ปีที่แล้ว +19

      @@deViant14 It's about knowing that there is only going to be one result, not that you know the result that so happens to be one of.

    • @jeremyself6984
      @jeremyself6984 3 ปีที่แล้ว +4

      @@mattmess1221 or if you don't care about the rest, *_

  • @beegdigit9811
    @beegdigit9811 3 ปีที่แล้ว +33

    7:11 A temp variable isn't required as you can do a one-liner: x = primes.intersection(evens).pop()
    Sure you can use this notation as it's shorter to type in some cases but I think it's better to use functions as they're more readable for someone who isn't familiar with the syntax.
    But this does make a very good trivia question as you need to decipher the meaning with knowledge about how comma-separated assignment works.

    • @mCoding
      @mCoding  3 ปีที่แล้ว +39

      Yeah, in real code I would definitely use pop. However, there is one benefit of ,= that I didn't mention. x, = singleton actually does length checking, ensuring that singleton has exactly one element, else it raises an exception. singleton.pop will happily give you an element even if singleton actually has more than one thing in it. Thanks for watching!

    • @beegdigit9811
      @beegdigit9811 3 ปีที่แล้ว +5

      @@mCoding Oh yeah, that's a great point!

    • @johnr3936
      @johnr3936 3 ปีที่แล้ว +4

      @@mCoding Thank you, this comment might be worth pinning.

    • @wandrespupilo8046
      @wandrespupilo8046 ปีที่แล้ว

      actually worth pinning

  • @ChristianGenco
    @ChristianGenco 3 ปีที่แล้ว +129

    Oh how weird. This apparently also works in Ruby!

    • @mCoding
      @mCoding  3 ปีที่แล้ว +32

      Good point! I wonder what other languages also have this oddity.

    • @Bunny99s
      @Bunny99s 3 ปีที่แล้ว +6

      Well, lua does not really have this behaviour, but a similar one. Lua has the concept of multivalue results. So a method can return a multi value result as a "single thing" and the syntax does just distribute the individual values to the individual variables. So
      function someFunc()
      return 3, 4
      end
      a, b = someFunc()
      As far as I remember you can not just use a comma, but you can use a dummy value if you wanted. Commonly the underscore is used. Though note that when you just do
      a = someFunc()
      you just get the value of 3. The only container type in lua is a table which can model arrays or objects. You can turn a multivalue result into an "array" / table just by doing
      a = {someFunc()}
      This is essentially the same as doing
      a = {3, 4}
      Methods also can have a special parameter called "...". This is essentially the var-args placeholder and is also a multi-value value ^^. Lua has methods to pack and unpack tables into multivalue values and back. Lua has just a few language features but they are incredible powerful. Though there are a few things that you will miss from other languages. For example, no "continue" for loops, you only get a "break" which sometimes makes structuring code a bit of a pain. Though it's an incredible powerful language.
      Another great example is the colon operator. While the normal dot "." indicates member access (like in most languages), the colon operator does the same with a slight twist. It allows syntactic sugar to essentially implement OOP with tables. Since functions are also just values and like all values they are essentially stored in a table (yes, even the local scope of a function is a table) you can do this:
      myTable:someFunc(1, 2, 3)
      The colon is just syntactic sugar for this:
      myTable.someFunc(myTable, 1, 2, 3)
      This is essentially how almost all OOP languages work under the hood. When you call an instance method the language simply has an implicit "self pointer" as first argument so the method knows its context. While you can work with closures to construct context specific methods, thanks to metatables we can actually implement inheritance. Metatables also allow you to override operator behaviour. Yes, it's not as typesafe as most strongly typed languages, but it's just fantastic what you can do with just a few language features. I have sucked up the whole lua specification :) I think there's not a single feature I don't funlly understand by now.

  • @ejon
    @ejon 3 ปีที่แล้ว +3

    I have been thinking of exactly this for many days now and here is your video explaining just that. THANK YOU!

  • @deviledlemur
    @deviledlemur 3 ปีที่แล้ว +77

    That actually seems very similar to JavaScript's array destructuring assignments. const [one, two] = [1, 2];

    • @mCoding
      @mCoding  3 ปีที่แล้ว +26

      That's exactly what it's doing!

    • @sahej97
      @sahej97 3 ปีที่แล้ว +23

      Called unpacking in python

    • @Nurutomo
      @Nurutomo 3 ปีที่แล้ว +5

      const [x] = y

    • @SophieJMore
      @SophieJMore 2 ปีที่แล้ว +1

      @Danko Kapitan Pretty sure python can do that with any iterable

  • @leogama3422
    @leogama3422 2 ปีที่แล้ว +7

    It remembers me of the "goes to" operator in C, as in "x goes to zero":
    while (x --> 0) // do stuff...
    (from a question in Stack Overflow)

    • @PedroContipelli2
      @PedroContipelli2 ปีที่แล้ว

      😂 Been a while since I've seen this one

  • @marekglowacki2607
    @marekglowacki2607 3 ปีที่แล้ว +41

    hah, I though that x ,= y is x = x,y so result should be (1,[2]) (like x+= y, is x = x + y) . I think there should be warning that ,= is not real operator, or "space" between should be enforced.

    • @duncanhw
      @duncanhw 3 ปีที่แล้ว +2

      Then x,= wouldn't work, even though could legitimately be x, =

    • @daddy3118
      @daddy3118 3 ปีที่แล้ว +1

      Comma is not an _operator_ of two arguments which is the case with the first character of true augmented assignments.

    • @marekglowacki2607
      @marekglowacki2607 3 ปีที่แล้ว +1

      @@daddy3118 yeah, sometimes syntax sugar is misleading, worse sometimes x+=y vs x=x+y in python is not syntax sugar ;)

    • @paulperkins1615
      @paulperkins1615 3 ปีที่แล้ว

      I think I would have preferred they make the parens around the left hand side tuple required instead of optional. Two more keystrokes, but much easier to see that it is a tuple assignment and not a simple value assignment.

  • @timonix2
    @timonix2 3 ปีที่แล้ว +15

    I guessed that it was going to unpack the list stored in y

  • @LaraiFox
    @LaraiFox 3 ปีที่แล้ว +6

    I recently started learning python myself for some small projects and I'm pretty chuffed that I managed to guess that x, = y would give 2 correctly. The reasoning behind my guess was that in Lua you can return multiple values from a function by using return value1, value2, etc... and to assign them to variables you can use commas. For example:
    x, y, z = some_function()
    In this case x, y, and z are each separate variables and get assigned the first three values returned from some_function. If you only wish to store some of the returned values but discard others you can use underscores as dummy variable where you want values to be discarded. For example:
    x, _, z = some_function()
    I guessed that perhaps in python a similar thing might be happening but where the underscores were not necessary and also in python's case the usage seemed to be extended to include assignment to multiple variables from a list. I didn't guess that the comma equals would need a space between them for proper formatting though. :P
    My long ramble aside, good video, very interesting, and thanks for sharing! ^_^

    • @lperkins2
      @lperkins2 ปีที่แล้ว +2

      The behavior with lua is very similar, but also slightly different. With lua, you truly are returning multiple values, so if you only capture the first, the rest are discarded. Python only allows returning a single object, but has a feature called automatic tuple (un)packing, which will take a sequence like `foo, bar` and turn it into a tuple for you. On the outside of the function, the process is reversed and the tuple (or other iterable) is unpacked. Unlike lua, the length of the iterable must match the number of left-hand-side targets.

  • @BtsRifer
    @BtsRifer 3 ปีที่แล้ว +1

    ",=" is so counterintuitive and error prompt. You can achieve the same functionality in a much more readable (and general) way:
    x, *_ = y

    • @nandakoryaaa
      @nandakoryaaa 3 ปีที่แล้ว

      yeah, like asterisk with underscore is more readable

  • @sophigenitor
    @sophigenitor 2 ปีที่แล้ว +5

    There are many instances where some function gives back a list and the correctness of the program depends on that list to have exactly one element which is extracted and used. The advantage of x, = y (or maybe better [x] = y) is that if y isn't exactly length 1, it throws an error.

    • @TheYxccxy
      @TheYxccxy ปีที่แล้ว

      I am not sure, but I would assume that
      y = [1, 2]
      [x] = y
      results in
      x == (1, 2)

    • @sophigenitor
      @sophigenitor ปีที่แล้ว +1

      @@TheYxccxy It's not, it throws an error. You can try it out.

  • @wickedprotos1937
    @wickedprotos1937 3 ปีที่แล้ว +56

    Some language operators just shouldn't exist. I avoid the 'clever' stuff for more straight forward that you can read and understand 2 years later.

    • @mCoding
      @mCoding  3 ปีที่แล้ว +18

      Totally agree, clarity over clever.

    • @wickedprotos1937
      @wickedprotos1937 3 ปีที่แล้ว +1

      @@mCoding Might mean I'm boring :-)

    • @hypnogri5457
      @hypnogri5457 3 ปีที่แล้ว +1

      @@wickedprotos1937 I’d say that this isn’t really intended per se. Probably just duck typing.

    • @wickedprotos1937
      @wickedprotos1937 3 ปีที่แล้ว

      I really didn't intend to be the operator troll :-) I'm by no means an expert. Love your videos.

    • @paulperkins1615
      @paulperkins1615 3 ปีที่แล้ว

      The operator is just assignment using "=". Then allow a tuple of assignment targets on the left hand side of the assignment instead of just one. And of course a tuple of one element is always valid, but needs a comma to show that it is not just a single parenthesized element. Then allow the parentheses around the tuple to be omitted. And of course spaces around a comma are optional. I think if it were my language I would not have made the parentheses optional.

  • @DrSpooglemon
    @DrSpooglemon 3 ปีที่แล้ว +4

    I have one thing to add to this. It is clear to me that parentheses do not create tuples under any circumstances. It is the commas alone which creates the tuple. The parentheses allow for the spreading across several lines or simply serve as a visual container to make things easier on the eyes. If everything is on one line the parentheses can be omitted entirely and the code would still work.

    • @nandoflorestan
      @nandoflorestan 3 ปีที่แล้ว +1

      An empty tuple is typically created by empty parentheses.

    • @DrSpooglemon
      @DrSpooglemon 3 ปีที่แล้ว

      @Ives Furtado The parentheses in those circumstances function as a container o separate what is inside them from everything else in the collection but it is still the commas that make the tuple. If you put a single integer in parentheses inside a list it will be stored as an integer not a tuple just as it would elsewhere although, yes you are correct, the parentheses under those circumstances are necessary.

    • @leogama3422
      @leogama3422 2 ปีที่แล้ว

      Parenthesis are optional for tuple literals and generator expressions depending on the context. If there's no ambiguity (i.e. if f the commas can't have any other meaning) you can drop them. The exception being the empty tuple literal.

    • @DrSpooglemon
      @DrSpooglemon 2 ปีที่แล้ว

      @@leogama3422 After re-reading my original comment I realise that parentheses would be necessary in order to create multiple tulpes in one line or creating a tuple inside a list. I got ahead of myself. I guess the certainty I wrote that with is an example of the Dunning-Kruger effect.

  • @M3t4lstorm
    @M3t4lstorm 3 ปีที่แล้ว +11

    It is much safer and has the same result to use:
    x, *_ = [1, 2]
    If you want to get the first element of a list, which may be empty, without doing a len check (Python: Do don't ask), you can use:
    first = next(iter([1, 2]), None)

    • @Lanxxe
      @Lanxxe 2 ปีที่แล้ว

      You can also do :
      lst[:1]
      right?
      It just returns the list if it's empty

    • @M3t4lstorm
      @M3t4lstorm 2 ปีที่แล้ว

      @@Lanxxe No, that returns a list

    • @demitri_t
      @demitri_t ปีที่แล้ว +2

      @@M3t4lstorm
      >>> x, *_ = []
      Traceback (most recent call last):
      File "", line 1, in
      ValueError: not enough values to unpack (expected at least 1, got 0)
      Python 3.10.2

  • @michelkluger
    @michelkluger 2 ปีที่แล้ว

    your videos improved a lot over time

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 3 ปีที่แล้ว +5

    This also means something:
    x = y,
    or even
    x, = y,
    Have you done one on the meaning of “[:]”? And how it differs on the LHS versus RHS of an assignment?

    • @mCoding
      @mCoding  3 ปีที่แล้ว +4

      I haven't done this yet but that's a good video idea! I hope you don't mind if I use it in the future!

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 3 ปีที่แล้ว

      @@mCoding By all means.

  • @ShankarSivarajan
    @ShankarSivarajan ปีที่แล้ว

    Thanks! I've been using this syntax for matplotlib things for years without understanding it.

  • @yusun5722
    @yusun5722 3 ปีที่แล้ว +35

    This is a nice alternative for x = y[0]. It's due to the fact that the comma defines a tuple hence no parenthesis is needed like (x,) = y. Great video.

    • @pu239
      @pu239 3 ปีที่แล้ว +11

      is there any reason to use x, = y instead of x = y[0] ?

    • @pu239
      @pu239 3 ปีที่แล้ว +1

      *any benefit

    • @xdjiijii6543
      @xdjiijii6543 3 ปีที่แล้ว +28

      @@pu239 no other than to piss off people that don't know it and make your code harder to read, but cmon it's fun :P

    • @luziferius3687
      @luziferius3687 3 ปีที่แล้ว +31

      @@xdjiijii6543 Wrong. *x = y[0]* extracts the first element of y and assigns it to x, where y only has to have *at least 1* element. *x, = y* does something similar, but is different. The second variant will fail on anything other than single-element iterables.
      So it also asserts that y *must* be a single-element iterable, i.e. assert len(y) == 1.
      If you assign y = [1, 2]
      then execute x, = y
      you’ll get a ValueError: too many values to unpack (expected 1)

    • @yusun5722
      @yusun5722 3 ปีที่แล้ว +3

      @@luziferius3687 Yes, x, = y assumes y has only one element.

  • @SJohnTrombley
    @SJohnTrombley ปีที่แล้ว

    Very cheeky, putting the space in the wrong spot, you almost got me.
    Seriously, this is a good argument in favor of PEP 8, the correct way to format that line is "x, = y".

  • @flowfian
    @flowfian 3 ปีที่แล้ว +9

    I have also seen some weird stuff where I had to pass a tuple of a single value, so I had to pass (x,)

    • @mCoding
      @mCoding  3 ปีที่แล้ว +1

      Single argument tuples are not that bad, what's crazy is unpacking one with x, = y.

    • @bobedgar6647
      @bobedgar6647 3 ปีที่แล้ว

      Needing and creating an iterable with a single value in it

  • @JonathanSwiftUK
    @JonathanSwiftUK 3 ปีที่แล้ว +27

    I think I'd rather 3-5 lines of code and be abundantly clear than use one and create a mystery / be unclear. I also still find subroutines weird, no obvious end unlike many other languages which might start and end them with some type of parentheses.

    • @Muzly
      @Muzly 3 ปีที่แล้ว +3

      There's an obvious end if you're familiar with Python.

    • @duncanhw
      @duncanhw 3 ปีที่แล้ว +1

      subroutines exist because they are the most basic call. Assembly uses subroutines because it's assembly. Every function call is secretly a subroutine call.

  • @adamhenriksson6007
    @adamhenriksson6007 3 ปีที่แล้ว +4

    Hint: ,= is not an actual operator.

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Bingo!

  • @sky4ce09
    @sky4ce09 3 ปีที่แล้ว

    got the first example right, wt the thought process "get n elements from a list, left to right, and add them all up"

  • @BrianStDenis-pj1tq
    @BrianStDenis-pj1tq 2 ปีที่แล้ว +1

    When I learned Python (not that I'm an expert), the comma was explained as "the tuple operator". That makes it pretty clear.

  • @maswinkels
    @maswinkels 3 ปีที่แล้ว

    An eight minute video for a simple syntax detail? Ain't nobody got time for that.

  • @valeriusandof9782
    @valeriusandof9782 3 ปีที่แล้ว +3

    I just discovered your channel and am binging all your videos. Great work!

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Glad you like them!

  • @user-tp5cx5vl9k
    @user-tp5cx5vl9k 3 ปีที่แล้ว

    It can be used when I want to get the one output of the one input in a tensorflow model.

  • @user-od2ld3jg9z
    @user-od2ld3jg9z 3 ปีที่แล้ว +4

    7:24
    You know you still can do it in one row
    x = primes.intersect(evens).pop()

  • @HansLemurson
    @HansLemurson 3 ปีที่แล้ว +5

    Space scan bem is leading. Thumb sup!

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Thanks!

  • @hotlaser
    @hotlaser 3 ปีที่แล้ว +23

    The explanation would be more complete by including the patterns x, _ = and x, *y =

    • @Fubbel42
      @Fubbel42 3 ปีที่แล้ว +1

      Yup, the latter in particular is quite handy if you just need the first (couple) values from a sequence and don't need the rest.

    • @jork8206
      @jork8206 3 ปีที่แล้ว +1

      I find myself using x, _ = or _, x = returns_tuple() quite a lot when using functions where the shape of the output is fixed. Tuple unpacking is pretty common in python and readable if you make it clear what's going on. To me, using x, = implies the size of the tuple might vary, where adding the underscore is a way to say "i don't care about the rest"

  • @jakubunold5750
    @jakubunold5750 ปีที่แล้ว

    interesting nuance! Gotta try it out, especially the exception when trying to deconstruct a container with more than one element. I guess if you need first element of a contaoner with multiple values then using x,_=... should do the trick...

  • @danielrhouck
    @danielrhouck 3 ปีที่แล้ว

    I didn't figure it out right away when you asked without the example (I wasn't watching at first, only listening), but I thought "that couldn't possibly be a thing, right? It's like the "goes down to" C operator -->, or something, not a real operator?".
    At 0:29 when you said "x is one, y is the list containing 2", I realized it had to be the one-element destructuring assignment operator. The RHS is a one-element sequence and it assigns that element to the LHS.
    All I can say to that is that it makes perfect sense and is simultaneously horrible nonsesnse.

  • @BiologyIsHot
    @BiologyIsHot 3 ปีที่แล้ว

    >be me expecting to learn a useful new syntactic sugar
    >Pikachu face

  • @phizc
    @phizc 3 ปีที่แล้ว +1

    I use C#, not Python, but it made me realize I could use deconstruction instead of temporary variables for swapping values.
    void Swap( T[] array, int a, int b ) {
    (array[a], array[b]) = (array[b], array[a]);
    }

  • @nathanaeltrimm2720
    @nathanaeltrimm2720 ปีที่แล้ว +1

    What’s the difference between the “.intersect()” method and the “&” set operator? (Aside from format)

  • @Mayur7Garg
    @Mayur7Garg 3 ปีที่แล้ว

    I actually got it right before watching the video. I had ruled out *Syntax error* based on common sense that no one is going to make about 8 min of video where the answer was going to be *Syntax error*.

  • @etopowertwon
    @etopowertwon 3 ปีที่แล้ว +9

    Oh. so it's similar to C's operator "goes to" (while(x-->0)). I'd never guess it.

    • @mCoding
      @mCoding  3 ปีที่แล้ว +4

      Bingo! It's not a real operator.

    • @lukefadedaway
      @lukefadedaway 3 ปีที่แล้ว

      --> is not an operator. while(x--) is the same Code with less characters

    • @ninesquared81
      @ninesquared81 3 ปีที่แล้ว +4

      @@lukefadedaway No, it's not the same code. Consider the initial value of x = -1. Then x is 'truthy', but x > 0 is clearly false, so the original code will never enter the while loop while yours will loop infinitely. Another example is the float/double x = 0.5. Here, indeed, x > 0, so the loop will execute once in the first case, then stop, but again infinitely in the second case.
      In any case, I personally find
      for (int i = whatever; i > 0; --i) { /* loop body */ }
      a lot clearer than both, albeit a bit more involved. Obviously I'm making the assumption here that the variable i is used only in the loop.

    • @etopowertwon
      @etopowertwon 3 ปีที่แล้ว +1

      @@lukefadedaway That's the point. ,= is not operator either.

    • @GRAYgauss
      @GRAYgauss 3 ปีที่แล้ว +1

      @@ninesquared81 If x is guaranteed to start as a positive int, the while(x--) will break too, but it won't behave similarly for negatives or any non-trivial decrementing refactor of course. He kinda missed the point of the OP though, and I feel like you probably made him recognize it. I personally also use for loops instead, it's not that much more effort and I often end up changing the conditional/incrementing anyways. The only time I actually use while is when I need a do. You can always do the int i; for(i = 0; i > 0; --i) case if you need i later, but that is getting back into the less clear territory perhaps.

  • @zodiacalcomet2097
    @zodiacalcomet2097 3 ปีที่แล้ว +1

    Huh, cannot say that I saw it before now. And reading the comments, it seems to be used in matplotlib.
    Saw the case and immediately thought of unpacking, though I admit I hesitated when you showed the fourth case.
    Good video by the way! Recently found your channel and all of them that I saw are really educational!

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Thanks, glad you enjoy!

  • @MElixirDNB
    @MElixirDNB 3 ปีที่แล้ว +3

    I think a good way to understand this is to look at unpacking, IE x, *_, y, z = ['some', 'array', ... ]

    • @mCoding
      @mCoding  3 ปีที่แล้ว +2

      Exactly!

  • @jord_n
    @jord_n 3 ปีที่แล้ว +2

    This reminds me of a piece of C++ code I've seen a long time ago, hopefully written as a joke. Try to understand the following:
    int n = 10;
    while(n --> 0)
    printf(n);
    If you refer to the --> as the "goes down to" operator, it's easy to be fooled into thinking it actually exists, because it seems to be doing exactly what you would expect.

    • @mCoding
      @mCoding  3 ปีที่แล้ว +2

      Totally agree! How much spacing matters!

  • @Shil_theangrybird
    @Shil_theangrybird 3 ปีที่แล้ว

    do u team from begenning?

  • @MakeNjoy
    @MakeNjoy 3 ปีที่แล้ว

    This syntax is used in the documentation for matplotlib.animation

  • @felipealvarez1982
    @felipealvarez1982 2 ปีที่แล้ว +11

    This is great. I also struggled with star operator. Mostly outside of functions (ie when not used for *args or **kwargs). I'm taking about tuple (sequence) unpacking. There are many cool uses of star operator that are esoteric but interesting. Can you cover this topic?

  • @arthank1263
    @arthank1263 ปีที่แล้ว

    I mean, it's an edge case, and it might be cool if they made an exception and let us have this as a shorthand for getting the first element in an interable

  • @re.liable
    @re.liable 3 ปีที่แล้ว +4

    I was wondering how you may unpack longer iterables (>1 element) using this. After trying, what I got is:
    iterable = range(5)
    x, *_ = iterable
    But maybe iterable[0] is just more explicit? Haha

    • @user-yw1hn7pt4y
      @user-yw1hn7pt4y 3 ปีที่แล้ว +1

      Indexing might not work with some iterables, it works with all sequences though. If you only need the first element, use next(iterable) (watch out for a StopIteration exception).

  • @Solarbonite
    @Solarbonite 3 ปีที่แล้ว +1

    I say x = 2 would make the most sense. Haven't seen video yet.
    Edit: Woot! Yup that made sense. At work we require standard code formatting at work as part of the IDE save :D.
    I had to think a bit but with the format it's somewhat clear what it's doing. A bit confusing though hahah.

  • @NuncNuncNuncNunc
    @NuncNuncNuncNunc 3 ปีที่แล้ว +1

    [x] = y seems like a much clearer statement, does not look like a typo, and is more consistent with other destructuring assignment uses.
    I don't know Python, but do that intersection, I think you'd want to do something similar to the above but ignore the 'rest', i.e. [x, *_] =

    • @rosiefay7283
      @rosiefay7283 3 ปีที่แล้ว

      [x] = y absolutely does look like a typo to me. The fact that it means something to the Python interpreter, I find bizarre. It's not at all clear what the assignment's destination is.

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 3 ปีที่แล้ว +6

    I think it's just tuple unpacking operator. Would be more accurate to say x, = y.

    • @matteopernarella
      @matteopernarella 3 ปีที่แล้ว +1

      yeah it is, if you try something like:
      x, = {1, 2}
      it throws an error saying that there are too many values to unpack.
      It's really not different from doing:
      x, y = {1, 2}
      using ',=' instead it's more difficult to read and prone to errors

  • @ugurunver2403
    @ugurunver2403 3 ปีที่แล้ว

    If you really need this, you should use like "x, __ = y"
    And there is a real life use case of this format in Django. get_or_create method returns (object, is_created) tuple and if you need only object you can do something like this:
    obj, __ = MyModel.objects.get_or_create(code=my_code, defaults={'value': my_val})
    obj will either be freshly created or fetched from existing record but if it doesn't matter for you in your scope, you can just ignore "created" parameter by using __

  • @Neuroszima
    @Neuroszima 2 ปีที่แล้ว

    so this unpacks the first value of iterable of the right side, and ignores the others, kind of like simulating the "x, z = [2]" but without typing "z" on the left side?
    If so, it isn't even the operator, it just assumes that we distribute right side into the tuple of arguments (that has only one argument), to the left side

  • @user-be3ls4mp5t
    @user-be3ls4mp5t 3 ปีที่แล้ว

    I didn't even know this operator exists.

  • @Sinke_100
    @Sinke_100 2 ปีที่แล้ว

    I don't think has anything to do with tuples, it's just like putting like a, b, c = [1,2,3], but instead of 3 elements u have one, but perhaps i might be wrong

  • @WolvericCatkin
    @WolvericCatkin 2 ปีที่แล้ว

    One use I can thing of: Concise bubble sort!

  • @JP-xe6gn
    @JP-xe6gn 3 ปีที่แล้ว +3

    Very interesting tip ! Never saw it in any other tutorial.

    • @taragnor
      @taragnor 3 ปีที่แล้ว +1

      Honestly, it's good to understand what it's doing, but it's not a convention you want to use in your code. Mainly because x,=y fails when y is larger than 1 element. So it is not a shortcut to writing x = y[0]

  • @hydropage2855
    @hydropage2855 3 ปีที่แล้ว +3

    "Is it a syntax error?". **IDE isn't showing it's a syntax error**

    • @mCoding
      @mCoding  3 ปีที่แล้ว +1

      Maybe I had syntax errors turned off!

    • @hydropage2855
      @hydropage2855 3 ปีที่แล้ว +2

      @@mCoding Figured that was possible, but didn’t think you’d bother, lmao

  • @Kaizzer
    @Kaizzer 3 ปีที่แล้ว +3

    I use that A LOT, for code style consistency reasons!
    I typically use it when decomposing values read from struct:
    (x, y, z) = struct.unpack_from('

    • @mCoding
      @mCoding  3 ปีที่แล้ว +1

      The parens make all the difference, thank you.

  • @rvmishra9881
    @rvmishra9881 3 ปีที่แล้ว +1

    Set of even and primes have 1 thing in common and that's two.

  • @geoafrikana
    @geoafrikana 2 ปีที่แล้ว +1

    I've never seen this before but I got it right. I'm proud of myself 😊
    That said, I think a more pythonic way to write this is x,_ = y.
    When unpacking tuples, an underscore is the recommended variable to use for any value you won't be using.

    • @mCoding
      @mCoding  2 ปีที่แล้ว +5

      x, _ = y will work if y has length 2, but it won't work if y has length 1 and we are trying to get the unique element out. You can do x, *_ = y that will work as long as y has at least 1 element, but it won't give the desired error if y actually has more than 1. You can manually check that y has length 1 first, but that only works if y supports len, which wouldn't work for generators for example.

    • @geoafrikana
      @geoafrikana 2 ปีที่แล้ว

      @@mCoding True. Thanks.

  • @knghtbrd
    @knghtbrd ปีที่แล้ว

    Had no idea what it'd do until you reformatted the code to x, = y … Then I'm like "ohhh! Yeah, I know exactly why that does what it does now."

  • @domints
    @domints 3 ปีที่แล้ว +1

    how about primes.intersection(even)[0]? Still you need one element and whatever you'd do you'd get first one without wonky syntax

    • @QuantumHistorian
      @QuantumHistorian 3 ปีที่แล้ว +1

      Sets are not subscritable, so this won't work and will return an error. Same thing with dictionaries, although what you suggested would work with lists and tuples. The advantage of ,= is you signal to the reader that the right hand side will only have 1 element no matter what. Or just confuse the hell out of them.

  • @hitk002cloudfull8
    @hitk002cloudfull8 3 ปีที่แล้ว

    Just use
    x = y[0]
    Also to get one value from list
    x , *z = y

  • @shivanshshalabh
    @shivanshshalabh 3 ปีที่แล้ว

    Great content 👍

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Thank you 🙌

  • @danielclv97
    @danielclv97 ปีที่แล้ว

    On the use case, can't you use x = primes.intersection(evens)[0] instead of x, = primes.intersection(evens)?

  • @shvechikov
    @shvechikov 3 ปีที่แล้ว +3

    The title is so misleading! But the video is good! :)

    • @mCoding
      @mCoding  3 ปีที่แล้ว +3

      But I tell you it's a trick question immediately! Thanks for the support!

  • @greenin5537
    @greenin5537 3 ปีที่แล้ว +1

    I guessed it! For me it looked like an unpacking statement (x,y = [2,1]) but just for one variable (x, = [2])

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Exactly, good job!

  • @2001herne
    @2001herne 3 ปีที่แล้ว

    Guess before watching: Some form of parameter unpacking - if y is a tuple, a, b = y would be equivilant to a = y[0]; b=y[1]. Therefore, x ,=y is equivilent to x=y[0]; ?=y[1] where ? is a throwaway.
    EDIT: Turns out that parameter unpacking is not whats going on. Pattern matching, I feel is both more and less powerfull. More powerfull for it's matching ability, but less powerfull because you cant just extract the first n items into distinct variables...

  • @startrunning4461
    @startrunning4461 ปีที่แล้ว

    back when I'm new to coding in python I usually put a comma at the end of each line. Until someone told me to not do that.

  • @dangnabbit1379
    @dangnabbit1379 2 ปีที่แล้ว

    Okaay, so also reading the comments seems to say that this is a struct oriented method overload to deconstruct a struct, as if the underlying is a pointer stack and you’re selecting a pointer from the top of the stack. That would be really useful generally speaking but I don’t know if an overloaded operator is type safe?

  • @japedr
    @japedr ปีที่แล้ว

    Also useful to get the only value of an iterator without having to wrap it in next(...)

  • @VincentKun
    @VincentKun 3 ปีที่แล้ว +2

    Functional programming syntax it's pattern matching

  • @Kamel419
    @Kamel419 3 ปีที่แล้ว +3

    exceptional video. to my surprise, i actually correctly predicted that it would consider x a tuple, but i didn't realize it would swap values between a tuple and a list. i've always said that what i hate most about python's syntax is implied tuples. in the very least, i think it should be required to use parenthesis i.e. (x ,)= y would have looked much less like an operator than x ,= y

  • @Bubu567
    @Bubu567 2 ปีที่แล้ว

    Lets say:
    var = [1,2,3]
    Then
    x,y,z = var
    This would assign 1 to x, 2 to y, and 3 to z.
    So x, = y, given y = [2], would assign 2 to x.

  • @xonxt
    @xonxt 2 ปีที่แล้ว

    It would've been a great little trick for "unsqueezing" a possible list, if it worked on a list of any size AND scalars, like if you had a function argument for example and you could do:
    x, = 1
    x, = [1]
    x, = [1, 2, 3]
    but since it doesn't do that and always requires an iterable of size 1... it kinda seems useless.

  • @IanWootten
    @IanWootten 2 ปีที่แล้ว

    A good demonstration of why code that hasn't been run through a linter shouldn't be accepted to your codebase

  • @aidanharper3784
    @aidanharper3784 3 ปีที่แล้ว

    Interesting, given that this necessitates a length check prior to calling anyway, what are the advantages of this over say, x = y[0] ?

    • @eneg_
      @eneg_ 3 ปีที่แล้ว

      You can't index a set and other similar types

  • @MasqueradeCrew
    @MasqueradeCrew 3 ปีที่แล้ว +4

    intersection.pop() is more readable, at least to me. Of course, I often have readability problems: unless variables, functions, and classes are really well-named, I'll have the hardest time figuring out what the sample of code does. It gets even harder when the brevity of the code is taken to an extreme, such as adding a comma after the variable name.

  • @alexb2773
    @alexb2773 ปีที่แล้ว

    I'm at 1:05 and I have to say, I have never seen this in my life. But based on the fact that a tuple is actually defined by the comma and not the brackets, I would go with option 4. Here goes nothing.
    Edit: wow!

  • @jamcdonald120
    @jamcdonald120 3 ปีที่แล้ว

    5:26, if you are using this syntax.... Dont; just use [0] like a normal person

  • @matheusaugustodasilvasanto3171
    @matheusaugustodasilvasanto3171 ปีที่แล้ว

    *x, = (...) is an interesting way to convert a tuple into a list, but it's probably slower than just list((...))

    • @mCoding
      @mCoding  ปีที่แล้ว

      Please profile and let me know the results!

  • @octubre_lilaka
    @octubre_lilaka 3 ปีที่แล้ว

    It actually means that python should hesitate before assigning the right value to the left

  • @appuser
    @appuser 2 ปีที่แล้ว

    X = y[0] is explicit and therefore my preferred option. Good to know how this works though for encountering smart-asses or code golf. Better yet, make Y a dict, with the value you want in X as the key or have a custom object with a member variable so it has a self-explaing name in the namepspace. Much clearer than having a list elements being used for different purposes, and could lead to much simpler functional programming style.

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Can confirm, a year later and I've yet to use ,= for any real purpose, favoring more explicit alternatives.

  • @CaptainCsaba
    @CaptainCsaba ปีที่แล้ว

    To be honest just putting [0] at the end would be much more readable than this syntax. But its a cool thing to know.

  • @nujuat
    @nujuat 3 ปีที่แล้ว

    Ayyy, I guessed it!

  • @Firigion
    @Firigion 3 ปีที่แล้ว +1

    The typical use case for this is matplotlib.pyplot.plot() returning a list of lines, but when you only drew one line and odn't want the list, you just do line, = plt.plot(...)

  • @BeesAndSunshine
    @BeesAndSunshine 3 ปีที่แล้ว

    What feels weirder than this quirk is the quirk that it works on sets (or sets of one a least), sets being unordered I would think that form of pattern matching shouldn't work on them at all.

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Under the hood, this sort of notation uses iterators, so you can put any iterable with one element on the right side!

  • @Veptis
    @Veptis ปีที่แล้ว

    So, I will not actually teach this when showing boolean operators. But I will make a find the error task where this syntax appears to test if the other trainers understand it.

  • @figloalds
    @figloalds 3 ปีที่แล้ว

    Awesome

  • @franklydoodle350
    @franklydoodle350 3 ปีที่แล้ว

    Funniest thumbnail I've seen in a while

    • @mCoding
      @mCoding  3 ปีที่แล้ว +1

      I felt so weird posing for this thumb. Glad it made you laugh!

  • @TeshanShanukaJ
    @TeshanShanukaJ 3 ปีที่แล้ว +4

    If you're new to python, this is a great new tip. But if you're somewhat familiar with python, thinking that's a syntax error might be the dumbest thing you've done

    • @luziferius3687
      @luziferius3687 3 ปีที่แล้ว

      It looks weird and deceiving by looking like any other compound assignment operator, which it is not.
      And is is a PEP8 code style violation when stated like that in the initial question. That’s why IDEs will flag those as style warnings, at least. If you follow the style guides closely, finding such things in other code bases can be really irritating.

  • @wristdisabledwriter2893
    @wristdisabledwriter2893 3 ปีที่แล้ว

    This is interesting but I’m curious is this the only language that does it

  • @mmohiey
    @mmohiey 3 ปีที่แล้ว

    here is another trick. if you want to get only the first element of a list that has more than one element:
    x, *_ = [1, 2, 3, 4]
    print(x) # this will give you 1

  • @haroldhamburgler
    @haroldhamburgler ปีที่แล้ว

    The comma operator simply returns item before it. So x ,= y should just set x to itself.

  • @ffgfghh5091
    @ffgfghh5091 3 ปีที่แล้ว

    Is this in sublime code?

    • @mCoding
      @mCoding  3 ปีที่แล้ว

      Actually, I'm using PyCharm here!

  • @user-ld8lc4ex4m
    @user-ld8lc4ex4m 11 หลายเดือนก่อน

    thank you

  • @joseville
    @joseville 3 ปีที่แล้ว +1

    Any way to make this syntax with the new (Python 3.8) assignment expression: `:=`???
    x, := y

    • @nallid7357
      @nallid7357 3 ปีที่แล้ว

      why

    • @tomduke558
      @tomduke558 3 ปีที่แล้ว

      @@nallid7357compress define and usage in the flavor of one liner...including the tuple unpacked
      instead of saying
      x, *_ = y(iterable)
      if x ....(whatever operator for conditionls)
      now it could be made as:
      if x, *_ := y
      (the second part of the tuple assignment is omitted if you know the iterable has just one element, thus x, := y will be the valid short form

    • @deadeye1982a
      @deadeye1982a 3 ปีที่แล้ว +1

      @@tomduke558 This will be a source for interesting new errors. If something was introduced, it can't be removed easily. You can still use the subscription to access the first element. Often it's not efficient to consume the whole list. The first element is assigned to the name "x" and a new created list with the rest of the items is assigned to the name "_". This is later garbage collected, but for a short time it's there. If your list is very long, this costs performance and memory.

    • @tomduke558
      @tomduke558 3 ปีที่แล้ว +1

      @@deadeye1982a yup regular index subscription to a list would be more explicite and memory friendly. agree that unpacking from long sequence will cause unneccessary garbage conglomeration

  • @HenrikMyrhaug
    @HenrikMyrhaug 3 ปีที่แล้ว

    So would x = [2] if you did:
    x, = y,
    ?
    I thought it would just set x to y.

  • @bettercalldelta
    @bettercalldelta ปีที่แล้ว

    if you write it like x, = y (like i do btw) it makes much more sense