#pythonoddity search results

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

searchsort's tweet image. 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โ€ฆ

#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.

datacascadia's tweet image. 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.

datacascadia's tweet image. 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โ€ฆ

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


No results for "#pythonoddity"

Riddle me this (python 3.6) #pythonoddity

__mharrison__'s tweet image. Riddle me this (python 3.6) #pythonoddity

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

searchsort's tweet image. One hint, the value of equals is different if you change the order of the arguments in the zip function ๐Ÿคฏ #pythonoddity

.@glasnt explaining one of my favorite Python oddities at @pycascades #pythonoddity #PyCascades2019

treyhunner's tweet image. .@glasnt explaining one of my favorite Python oddities at @pycascades #pythonoddity #PyCascades2019
treyhunner's tweet image. .@glasnt explaining one of my favorite Python oddities at @pycascades #pythonoddity #PyCascades2019
treyhunner's tweet image. .@glasnt explaining one of my favorite Python oddities at @pycascades #pythonoddity #PyCascades2019
treyhunner's tweet image. .@glasnt explaining one of my favorite Python oddities at @pycascades #pythonoddity #PyCascades2019

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

treyhunner's tweet image. 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

treyhunner's tweet image. ๐Ÿ 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

datacascadia's tweet image. 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

#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 ?

TonyFlury's tweet image. #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!?

datacascadia's tweet image. 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โ€ฆ

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.

datacascadia's tweet image. 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.

Loading...

Something went wrong.


Something went wrong.


United States Trends