#pythonoddity search results
One hint, the value of equals is different if you change the order of the arguments in the zip function ๐คฏ #pythonoddity

In Python 3.12, f-strings gained quite a bit more flexibility. You couldn't do THIS in Python 3.11: >>> f"{F"{f"{F"{"""""""""f"""""""""""}"}"}"}" 'f' #pythonoddity
Comprehensions have their own scope: >>> n = 4 >>> squares = [n**2 for n in range(9)] >>> n 4 But loops do NOT have their own scope: >>> n = 4 >>> for n in squares: ... >>> n 81 This looks like a #PythonOddity, but there's a good reason. #Python trey.io/function-scopeโฆ
![treyhunner's tweet image. Comprehensions have their own scope:
>>> n = 4
>>> squares = [n**2 for n in range(9)]
>>> n
4
But loops do NOT have their own scope:
>>> n = 4
>>> for n in squares: ...
>>> n
81
This looks like a #PythonOddity, but there's a good reason. #Python
trey.io/function-scopeโฆ](https://pbs.twimg.com/media/Gc4H0xfXoAAau7c.jpg)
#pythonoddity A little of Python and math: You are probably familiar with the module operation (%). 4 % 0 is 2, easy, 7 % 6 is 1, easy too. What about -7 % 6? It's not -1, it's 5! Why? There are several definitions for module, Python follows the floored division definition.
math.isclose(a, b) should be used here instead to see the degree of tolerance. Using equal sign to floating point representation is not the best thing to do. #pythonoddity
Another #pythonoddity maybe someone wants to take a crack at? @driscollis @mathsppblog I get that python only has so many digits in the float built in data type, but the threshold seems... odd.

If you pass None to the slice function, you select all elements of the sequence ๐ค my_list = [1, 2, 3, 4] my_slice = slice(None) print(my_list[my_slice]) # [1, 2, 3, 4] #Python #pythonprogramming #pythonoddity
Be careful multiplying lists of mutable objects though: >>> matrix = [[0] * 3] * 3 >>> matrix[1][1] = 1 >>> matrix [[0, 1, 0], [0, 1, 0], [0, 1, 0]] This #pythonoddity happens because Python's data structures don't actually "contain" objects. pym.dev/pointers/#dataโฆ
pythonmorsels.com
Variables and objects in Python
Unlike many programming languages, variables in Python are not buckets which "contain" objects. In Python, variables are pointers that "point" to objects.
I've collected many "Python oddities" (#pythonoddity) over the years, which are code snippets that may surprise new Pythonistas. This term has confused folks who thought I meant "bug" instead of "potential gotcha". What should I name the repo I'll collect these in?
Wish for symmetry in your index negations? Instead of 0 being first and -1 last we'd use 0 first and -0 last. This doesn't work because 0 == -0 and... well, number lines! Solution: use ~! ~0 == -1 ~1 == -2 etc. You're welcome. (but don't do this ๐ฌ) #Python #PythonOddity
Just wrote up an explanation of one of my favorite bits of Python to teach. It's a useless bit of code that's nonetheless helpful to understand. >>> x = [] >>> x.append(x) The question to ponder: what's x? The explanation: github.com/treyhunner/pytโฆ #Python #PythonOddity
Talking about Python gotchas & oddities right now #PythonOddity #Python x.com/i/spaces/1mrxmโฆ
Python scope oddity ๐โ N = [1, 2, 3] def set_N(*nums): print(N) N = nums >>> set_nums([4, 5, 6]) UnboundLocalError: local variable 'N' referenced before assignment Names can't be both local and global in the same scope. ๐ฎ #pythonoddity
Python's pathlib.Path allows using / to to join paths. That also allows /= to work, which allows for some slightly odd-looking code. >>> from pathlib import Path >>> directory = Path() >>> directory /= "Documents" >>> directory PosixPath('Documents') #Python #PythonOddity
A #pythonoddity that abuses #Python's class construction system: >>> from itertools import zip_longest >>> class Zip(zip_longest("zip!")): "zip?" ... >>> print(*next(zip(*Zip)), sep="") Zip Creating a new class pretty much does this: type(base_type)(name, bases, attr_dict)
Yet another way, with an assignment expression within an assignment expression: >>> from itertools import count, islice >>> a = b = 1 >>> numbers = ((b:=a+(a:=b))-a for _ in count()) >>> list(islice(numbers, 12)) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144] #Python #pythonoddity
Python's data structures do NOT contain objects. They contain pointers to objects. To demonstrate this fact, at the end of this video I show a sort of ouroboros: a list that "contains" itself. #Python #pythonoddity pythonmorsels.com/data-structureโฆ
๐ Python string oddity ๐ >>> "โ โโ ฃโ งโโ ช".isnumeric() True >>> "โ ฌโ ญโ ฎโ โ ฏยณโโ".isnumeric() True >>> "-3".isnumeric() False #pythonoddity #python
An in-place addition #pythonoddity ๐โ Given a list (a) and a tuple (b): a = [1, 2] b = (1, 2) These all work: a += b a += a b += b But this raises an exception: b += a TypeError: can only concatenate tuple (not "list") to tuple
I tend to avoid #Python's number-related string methods #pythonoddity def check(s): return s.isnumeric(), s.isdigit(), s.isdecimal() >>> check("โ ค") (True, False, False) >>> check("โบ") (True, True, False) >>> check("๐") (True, True, True) >>> check("-5") (False, False, False)
I've never thought to try this! I wonder what #pythonoddity situations this could lead to. ๐ค
Python's pathlib.Path allows using / to to join paths. That also allows /= to work, which allows for some slightly odd-looking code. >>> from pathlib import Path >>> directory = Path() >>> directory /= "Documents" >>> directory PosixPath('Documents') #Python #PythonOddity
Implementing a singleton in #Python, the wrong way: >>> from functools import cache >>> @cache ... class Thing: ... def __init__(self, name): ... self.name = name ... >>> x = Thing("x") >>> x is Thing("x") True #PythonOddity
Wish for symmetry in your index negations? Instead of 0 being first and -1 last we'd use 0 first and -0 last. This doesn't work because 0 == -0 and... well, number lines! Solution: use ~! ~0 == -1 ~1 == -2 etc. You're welcome. (but don't do this ๐ฌ) #Python #PythonOddity
math.isclose(a, b) should be used here instead to see the degree of tolerance. Using equal sign to floating point representation is not the best thing to do. #pythonoddity
Another #pythonoddity maybe someone wants to take a crack at? @driscollis @mathsppblog I get that python only has so many digits in the float built in data type, but the threshold seems... odd.

Comprehensions have their own scope: >>> n = 4 >>> squares = [n**2 for n in range(9)] >>> n 4 But loops do NOT have their own scope: >>> n = 4 >>> for n in squares: ... >>> n 81 This looks like a #PythonOddity, but there's a good reason. #Python trey.io/function-scopeโฆ
![treyhunner's tweet image. Comprehensions have their own scope:
>>> n = 4
>>> squares = [n**2 for n in range(9)]
>>> n
4
But loops do NOT have their own scope:
>>> n = 4
>>> for n in squares: ...
>>> n
81
This looks like a #PythonOddity, but there's a good reason. #Python
trey.io/function-scopeโฆ](https://pbs.twimg.com/media/Gc4H0xfXoAAau7c.jpg)
A playlist of Python gotcha-related talks And notes from my talk: trey.io/oddities Also see the #PythonOddity hashtag. youtube.com/playlist?list=โฆ
Talking about Python gotchas & oddities right now #PythonOddity #Python x.com/i/spaces/1mrxmโฆ
Just wrote up an explanation of one of my favorite bits of Python to teach. It's a useless bit of code that's nonetheless helpful to understand. >>> x = [] >>> x.append(x) The question to ponder: what's x? The explanation: github.com/treyhunner/pytโฆ #Python #PythonOddity
An explanation of these faces ๐ง๐ณ๐ฎ >>> O_o = 0_0 >>> o_O = 0,0 >>> o_o = 0o0 >>> O,O = o_O >>> O_o, o_o, O,O, *o_O (0, 0, 0, 0, 0, 0) #PythonOddity github.com/treyhunner/pytโฆ
I'm pleased that this #PythonOddity response ๐ x.com/treyhunner/staโฆ
Python faces #pythonoddity >>> O_o = 0_0 >>> o_O = 0,0 >>> o_o = 0o0 >>> O,O = o_O >>> O_o, o_o, O,O, *o_O (0, 0, 0, 0, 0, 0)
Python's Decimal module allows you to add wings around your numbers: >>> from decimal import Decimal >>> Decimal("__1__") Decimal('1') Is this a bug?... or a feature! >>> Decimal("____0____.____0____") Decimal('0.0') #PythonOddity #Python
I've collected many "Python oddities" (#pythonoddity) over the years, which are code snippets that may surprise new Pythonistas. This term has confused folks who thought I meant "bug" instead of "potential gotcha". What should I name the repo I'll collect these in?
Python's data structures do NOT contain objects. They contain pointers to objects. To demonstrate this fact, at the end of this video I show a sort of ouroboros: a list that "contains" itself. #Python #pythonoddity pythonmorsels.com/data-structureโฆ
Just learned this fun #Python Easter Egg from @llanga & @pyblogsal's core.py podcast. >>> import math >>> hash(math.inf)/100_000 3.14159 Also if you've enjoyed my #pythonoddity posts in the past... give this latest episode (Episode 12) a listen!
A #pythonoddity that abuses #Python's class construction system: >>> from itertools import zip_longest >>> class Zip(zip_longest("zip!")): "zip?" ... >>> print(*next(zip(*Zip)), sep="") Zip Creating a new class pretty much does this: type(base_type)(name, bases, attr_dict)
Yet another way, with an assignment expression within an assignment expression: >>> from itertools import count, islice >>> a = b = 1 >>> numbers = ((b:=a+(a:=b))-a for _ in count()) >>> list(islice(numbers, 12)) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144] #Python #pythonoddity
#pythonoddity A little of Python and math: You are probably familiar with the module operation (%). 4 % 0 is 2, easy, 7 % 6 is 1, easy too. What about -7 % 6? It's not -1, it's 5! Why? There are several definitions for module, Python follows the floored division definition.
I stumbled upon an interesting #pythonoddity yesterday. ๐ ๐ค Can you spot the bug in this #Python code? values = ["Trey", "Hunner", "4"] expected = ( "First,Last,Number\r\n" ",".join(values) + "\r\n" )
In Python 3.12, f-strings gained quite a bit more flexibility. You couldn't do THIS in Python 3.11: >>> f"{F"{f"{F"{"""""""""f"""""""""""}"}"}"}" 'f' #pythonoddity
๐ Python string oddity ๐ >>> "โ โโ ฃโ งโโ ช".isnumeric() True >>> "โ ฌโ ญโ ฎโ โ ฏยณโโ".isnumeric() True >>> "-3".isnumeric() False #pythonoddity #python
One hint, the value of equals is different if you change the order of the arguments in the zip function ๐คฏ #pythonoddity

Hey @PyGotham! ๐ I'm giving a talk on #Python Oddities at 11:15am in PennTop South. #pythonoddity ๐๐ค If you want a @PythonMorsels sticker, come find my after my talk. ๐๐ #PyGotham

๐ Python string oddity ๐ >>> "โ โโ ฃโ งโโ ช".isnumeric() True >>> "โ ฌโ ญโ ฎโ โ ฏยณโโ".isnumeric() True >>> "-3".isnumeric() False #pythonoddity #python

Nice sleuthing! Is there some rule about when __eq__ uses == and when it uses is? It seems like that the two can differ even within one type of object, like with this #pythonoddity

in-place addition #pythonoddity ๐โ Given a list and a tuple: lst = [1, 2] tpl = (1, 2) These all work: lst += tpl lst += lst tpl += tpl This raises a TypeError: tpl += lst TypeError: can only concatenate tuple (not "list") to tuple
![treyhunner's tweet image. in-place addition #pythonoddity ๐โ
Given a list and a tuple:
lst = [1, 2]
tpl = (1, 2)
These all work:
lst += tpl
lst += lst
tpl += tpl
This raises a TypeError:
tpl += lst
TypeError: can only concatenate tuple (not "list") to tuple](https://pbs.twimg.com/media/FJA5pGHX0AQJk_4.jpg)
#PythonOddity @treyhunner @raymondh @brettsky @gvanrossum Method on a metaclass is callable through a concrete class - but hasattr returns False - happens on Py3.5 & Py3.6 - bug ? or by design ?

Even weirder things happen if you try using the Int64 dtype, arguably the correct one for ints with missing valuesโฆ The np.nan == np.nan doesnโt return True OR False ๐คฏ #pythonoddity Maybe your amazing pandas trick is why this dtype hasnโt been more widely adopted!?

Comprehensions have their own scope: >>> n = 4 >>> squares = [n**2 for n in range(9)] >>> n 4 But loops do NOT have their own scope: >>> n = 4 >>> for n in squares: ... >>> n 81 This looks like a #PythonOddity, but there's a good reason. #Python trey.io/function-scopeโฆ
![treyhunner's tweet image. Comprehensions have their own scope:
>>> n = 4
>>> squares = [n**2 for n in range(9)]
>>> n
4
But loops do NOT have their own scope:
>>> n = 4
>>> for n in squares: ...
>>> n
81
This looks like a #PythonOddity, but there's a good reason. #Python
trey.io/function-scopeโฆ](https://pbs.twimg.com/media/Gc4H0xfXoAAau7c.jpg)
Another #pythonoddity maybe someone wants to take a crack at? @driscollis @mathsppblog I get that python only has so many digits in the float built in data type, but the threshold seems... odd.

Something went wrong.
Something went wrong.
United States Trends
- 1. Flacco 81.8K posts
- 2. Bengals 78.7K posts
- 3. Bengals 78.7K posts
- 4. Tomlin 21.2K posts
- 5. Ramsey 18.5K posts
- 6. Chase 107K posts
- 7. Chase 107K posts
- 8. #WhoDey 6,691 posts
- 9. #TNFonPrime 5,543 posts
- 10. Teryl Austin 2,679 posts
- 11. #WhoDidTheBody 1,352 posts
- 12. #PITvsCIN 6,954 posts
- 13. Cuomo 79.2K posts
- 14. #criticalrolespoilers 6,265 posts
- 15. Max Scherzer 13.9K posts
- 16. Andrew Berry 3,138 posts
- 17. Burrow 9,549 posts
- 18. DK Metcalf 3,922 posts
- 19. Zac Taylor 2,509 posts
- 20. Ace Frehley 93.6K posts