How is THIS a thing in Python?!

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 60

  • @sarimbinwaseem
    @sarimbinwaseem 5 днів тому +32

    I'll definitely use (1,)in((1,),) in real world instead of while True....

  • @Yvant2000
    @Yvant2000 4 дні тому +15

    Also works with imaginary numbers !
    3jor 1 returns (0 + 3i)

  • @__Brandon__
    @__Brandon__ 4 дні тому +14

    A good follow up video would be about what that guy was doing to get upset over them making it a warning

    • @Carberra
      @Carberra  4 дні тому +4

      Honestly I was thinking the same thing when I read it! Looks like it's obfuscation work mainly, which I guess is actually a valid, if not bizarre, use case for it.
      The thread on that issue is pure gold though, reference [5] if you wanna look for yourself!

    • @JonnyRobbie
      @JonnyRobbie 2 дні тому

      That guy was upset over a technicality with hilariously linear thinking. He IS technically right that right now, the syntax is supported and I do see his point that you should’n make a warning something that is technically supported. What he failed to realize that the warning has some sort of future (to be made into an error) meaning.

  • @MirrorsEdgeGamer01
    @MirrorsEdgeGamer01 3 дні тому +5

    Fun fact, the syntax highlining library, that Discords uses, knows about the optional whitespace issue. What this means is that syntax highlighting properly highlights it. I am not sure if this was intentional or not. I am guessing the latter.

  • @Bob-1802
    @Bob-1802 5 днів тому +15

    Reminds me we should read "The zen of Python" few more times 🤗

  • @fyellin
    @fyellin 4 дні тому +5

    I did a search through a large dictionary of words. There really isn’t anything common except for “decor” and the Russian name “Fedor”.

  • @drdca8263
    @drdca8263 4 дні тому +3

    1:03 : the third one is just expected behavior? What’s weird about it?
    2:48 : ah, ok, yes it is missing the white space, sure

    • @TheWarTurkey
      @TheWarTurkey 2 дні тому +1

      Yea, I thought the 3rd one seemed fine, too.

  • @anon_y_mousse
    @anon_y_mousse 4 дні тому +4

    That kind of short circuit evaluation annoys me because it does create these weird ambiguities if you're not reading carefully. However, the third example makes perfect sense because (1) will be interpreted as just 1 and (1,) will be evaluated as a tuple containing a single item which is 1. This also really shows the inspiration of early BASIC's in Python's design.

    • @phobos.anomaly
      @phobos.anomaly 4 дні тому

      Short circuit evaluation is when B is not evaluated in A or B if A is True.
      This is not that, it's just some lenient parsing which happens to allow some unfortunate things.

    • @anon_y_mousse
      @anon_y_mousse 4 дні тому

      @@phobos.anomaly '0': start of a number; 'x': mode switch to hex number; '_': continuation of a number, used as visual spacing; 'f': hex character, we've now got 15; 'o': not hex, short circuit here, return to normal mode;

    • @phobos.anomaly
      @phobos.anomaly 4 дні тому

      @@anon_y_mousse "short circuit evaluation" means a very specific thing in the context of programming languages, and this is not it. Look it up.

    • @anon_y_mousse
      @anon_y_mousse 4 дні тому

      @@phobos.anomaly This is one of the contexts in which it is applied.

    • @notalakelurk
      @notalakelurk 3 дні тому

      ​@@phobos.anomaly, this user is complaining about short circuit evaluation, though. Take the first example: `0x_for x in [1, 2, 3]`. There are three parts that python sees here (as you said, leniency): `0x_f`, `or`, and `x in [1, 2, 3]`. Python evaluates `0x_f` to be a non-zero number (i.e. truthy), then it gets to the `or` statement; since it already has a truthy value (0x_f) it short circuits here, never evaluating `x in [1, 2, 3]`.

  • @dandyexplorer4252
    @dandyexplorer4252 5 днів тому +6

    Did you write the second example with multiplication symbol instead of 'x'?

    • @FN64qHCnbokMG2L
      @FN64qHCnbokMG2L 5 днів тому

      nope, he's just FaNcY and uses a font with ligatures that prettyprints, so to say, stuff like "0xdeadbeef"

    • @Carberra
      @Carberra  5 днів тому +1

      I like being FaNcY! But yes, it is ligatures.

    • @dandyexplorer4252
      @dandyexplorer4252 5 днів тому +1

      @@Carberra What do you mean? That you wanted people to think it is 'x' and not *?

    • @lietpi
      @lietpi 5 днів тому

      ​@@dandyexplorer4252 no, ligatures. A special font feature

    • @Nesdac-k1l
      @Nesdac-k1l 4 дні тому

      ​@@dandyexplorer4252the font they are using has a ligature, which is: when 'x' is in a hexadecimal number, it will be made a little smaller and centred vertically, like '×'. its the same character, the font is just rendering it slightly different. many people (including me) consider this to be a little nicer to read. idk what font it is, but Fira Code does exactly this so it might be that.

  • @davidmurphy563
    @davidmurphy563 5 днів тому +8

    That thumbnail gave me a headache...

  • @andrew_ray
    @andrew_ray 4 дні тому +3

    What happens with `0x_band saw`?

    • @Carberra
      @Carberra  4 дні тому

      It will error unless the variable "saw" is defined as it will check both. If it's defined, it'd be fine though!

    • @andrew_ray
      @andrew_ray 4 дні тому +1

      @@Carberra That's wild, because that means Python can't be parsed with a linear parser.

    • @zTJq40sl
      @zTJq40sl 4 дні тому +1

      @@andrew_ray Evaluating an undefined variable is an error, but not a syntax error. Thus, while I don't know whether Python can be parsed by a linear parser, I don't think the short-circuiting behavior or `and` and `or` has any influence of what parsers can be used, as long as parsing and evaluating are separate steps.

    • @andrew_ray
      @andrew_ray 3 дні тому +3

      @@zTJq40sl A linear parser would have no way of knowing that it should stop parsing the hex literal after 0x_b. It would have to tokenize that sequence as "0x_ba/nd/ /saw" which would result in a syntax error since "nd" isn't a valid operator.

    • @dandyexplorer4252
      @dandyexplorer4252 3 дні тому

      On my PC, when I run it with Python 3.12.0, it gives 'SyntaxError: invalid hexadecimal literal', no matter if 'saw' is defined or not.

  • @JonathanDenning
    @JonathanDenning 6 годин тому

    i teach programming languages at university, and i started collecting these a few years ago. i call them coding shenanigans. this is getting added!

    • @Carberra
      @Carberra  5 годин тому

      Care to share a few of your favourites? I'd be interested to know!

    • @JonathanDenning
      @JonathanDenning 3 години тому

      @Carberra here's one...
      >>> (False == False) in [False]
      False
      >>> False == (False in [False])
      False
      >>> False == False in [False] # what does this print?
      ???

  • @mage1over137
    @mage1over137 День тому

    So these are basically puns in python

  • @isodoublet
    @isodoublet День тому

    So basically python developers are so traumatized by the 2 -> 3 transition that they're afraid to fix obvious parser bugs

    • @Carberra
      @Carberra  День тому

      Not quite, Python 3.10 introduced features with syntax not possible using LL(1). I think the match statement? Something about the use of soft keywords iirc.

    • @isodoublet
      @isodoublet День тому

      @@Carberra Do any of those changes break previously working code (even if it obviously shouldn't code, as is the case with the examples given in this video)?

  • @thisisaplaceholdernamedont6980
    @thisisaplaceholdernamedont6980 3 дні тому +1

    python is clearly not following the zen of python at this point

  • @PixelOutlaw
    @PixelOutlaw 4 дні тому +3

    Sloppy tokenization.

  • @alchenerd
    @alchenerd 2 дні тому

    I don't know what possessed me but here's a funny thing:
    ```
    from warnings import filterwarnings as enemies # We do not overlook enemies
    enemies('ignore') # Instead, we silence them
    pledge = bool # Our pledge is resolute
    pledge(0x_for honor_x0) # The moment of truth: are we people of honor?
    ```

  • @cn-ml
    @cn-ml 4 дні тому

    Sorry, but wtf is a syntax *warning* ?? Either it is correct syntax or it is not. What is going on here?

    • @Carberra
      @Carberra  4 дні тому +6

      SyntaxWarnings are typically used to warn people of deprecated syntax, and become SyntaxErrors in later versions.

    • @phobos.anomaly
      @phobos.anomaly 4 дні тому +1

      It's syntax that's correct but considered unwanted.
      What else are C compiler warnings? They're valid constructs that are probably a bad idea and warrant another look.

    • @cn-ml
      @cn-ml 4 дні тому

      @phobos.anomaly ah you are right, I totally missed that. In any case I *strongly hoped* that this thing here is unambiguously classified as a syntax error

    • @phobos.anomaly
      @phobos.anomaly 4 дні тому

      @@cn-ml Oh I agree, allowing this syntax is bad for everyone (ok except obfuscated code competitions :P).
      But disallowing it is backwards incompatible, so I get they want to be careful about it. I hope the warning paves the road for turning it into an error in the future.

    • @AlexanderVulpes
      @AlexanderVulpes 3 дні тому +1

      SyntaxWarning is a weird concept for a weird situation. It's basically "this syntax is not supposed to be valid, but the interpreter accepts it, and if we make the interpreter raise a SyntaxError like it should, things break"

  • @airatvaliullin8420
    @airatvaliullin8420 5 днів тому +7

    Hope this helps :)
    >>> from pathlib import Path
    >>> words = Path("common-words.txt").read_text().split("
    ")
    >>> len(words)
    5000
    >>> HEX_LETTERS = set("abcdef")
    >>> hex_words = [word for word in words if set(word) >> hex_words
    ['face', 'feed', 'dead', 'deed']

    • @sarimbinwaseem
      @sarimbinwaseem 5 днів тому +3

      Looks cool... need to find common-words.txt file somewhere...

    • @airatvaliullin8420
      @airatvaliullin8420 5 днів тому

      @sarimbinwaseem Google for "English words txt"!

    • @Carberra
      @Carberra  5 днів тому +2

      Aah, that's cool! Would be interesting to use that to see how many possible words there actually are (including those that end in "or").

    • @aaaaaa2493
      @aaaaaa2493 4 дні тому

      The most memorable for me is 0xCAFEBABE which is first four bytes of the Java class file)

    • @desertfish74
      @desertfish74 4 дні тому +1

      Deadbeef c0dedbad