Arama Sonuçları: "#pythonoddity"

20 sonuç
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
546
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
426
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
378
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
951
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
1.4K
Trey Hunner
Trey Hunner@treyhunner·
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?
English
0
0
0
740
Trey Hunner
Trey Hunner@treyhunner·
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!
English
0
0
0
746
Trey Hunner
Trey Hunner@treyhunner·
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)
English
0
0
0
3K
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
325
Santos Gallegos
Santos Gallegos@stsewd·
#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.
English
0
0
0
123
Trey Hunner
Trey Hunner@treyhunner·
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" )
English
0
0
0
3.1K
Trey Hunner
Trey Hunner@treyhunner·
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
English
0
0
0
7.2K
Python Morsels
Python Morsels@PythonMorsels·
🐍 Python string oddity 👇 >>> "⅐ↁⅣⅧↂⅪ".isnumeric() True >>> "ⅬⅭⅮ⅟Ⅿ³₉ↀ".isnumeric() True >>> "-3".isnumeric() False #pythonoddity #python
English
0
0
0
626
Daniel Mesejo
Daniel Mesejo@searchsort·
One hint, the value of equals is different if you change the order of the arguments in the zip function 🤯 #pythonoddity
Daniel Mesejo tweet media
English
0
0
0
66