Tweets
- Tweets, current page.
- Tweets & replies
- Media
You blocked @raymondh
Are you sure you want to view these Tweets? Viewing Tweets won't unblock @raymondh
-
Your move:
@dabeaz@ramalhoorg@dontusethiscode 3/ * Reposted with spaces instead of tabsShow this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
Let the games begin! >>> d = Dict( cm = classmethod(lambda cls: cls.__name__.upper()), pr = property(lambda self: self['x'] ** 2), x = 5, ) >>> d['cm']() 'DICT' >>> d['pr'] 25 >>> d['x'] = 6 >>> d['pr'] 36 2/
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
Egregious
#Python hack of the day: Add descriptor logic to dictionaries. class Dict(dict): def __getitem__(self, key): value = super().__getitem__(key) if hasattr(value, '__get__'): return value.__get__(self, type(self)) return value 1/Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
The @cached_property() decorator only works if there is a writeable instance dictionary, so it won't work with __slots__ or metaclasses. Also the writeback does not play well with PEP 412 key-sharing dicts, so you lose the space savings. 5/
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
For cached_property(), being a non-data descriptor is the key to how it works. When it is first called, the result is written back to the instance dictionary. Subsequent calls find the attribute instead of the cached property. 4/
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
>>> d = {'__get__', '__set__', '__delete__'} >>> d & set(dir(property)) {'__set__', '__get__', '__delete__'} >>> d & set(dir(lambda : None)) {'__get__'} >>> d & set(dir(classmethod)) {'__get__'} >>> d & set(dir(cached_property)) {'__get__'} 3/
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
class A:
@property def p(self): return 'boop' def m(self): return 'pop' >>> a = A() >>> a.p 'boop' >>> a.m() 'pop' >>> vars(a)['p'] = 'hiss' >>> vars(a)['m'] = 'bonk' >>> a.p # instance variable ignored 'boop' >>> a.m # instance variable honored 'bonk' 2/Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
#Python tip: A descriptor defining __set__() or __delete__() is considered a data descriptor and has precedence over an instance variable with the same name. property() is a data descriptor. Function, classmethod, staticmethod, and cached_property are non-data descriptors. 1/Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
5/ People are always asking about how to kill another thread. We don't provide an API for that. Reason: you never know when that thread is holding a lock. But if you're clever, you can use ctypes to kill it. Afterwards, feel free to contract me for help with the deadlocks.
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
4/ Integers are immutable in
#Python, but if you're clever, ctypes can be used to alter the value field. Nothing good can come of this.
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
3/ Once upon a time, True and False weren't keywords. You could write: True, False = False, True and watch the world fall apart. Ultimately, it was decided that constants should be constant.
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
2/ Today a Pythonista wanted to how to make "C() is None" always return True, by overriding
#Python's is-operator.@ramalhoorg correctly advised that this would be "catastrophic". This conversation is still continuing as people look for ways to lie about object identity.
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
1/ Many years ago, I maintained General Ledger software. A customer wanted to know how to disable the debits==credits check. We strongly advised against it. Never the less, the customer demanded to know. A month later, they called back saying their books were out of balance
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
5/ The
@cache decorator is new in#Python version 3.9. Prior to that, you can get the same effect with an lru_cache where the maxsize is set to None:@lru_cache(maxsize=None) def f(x): ... So, really@cache is just a pretty way to spell it.Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
4/ It's implemented with fast C code, equivalent to: key = make_key(args, kwds, typed) result = cache_get(key, sentinel) if result is not sentinel: hits += 1 return result misses += 1 result = user_function(*args, **kwds) cache[key] = result return result
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
3/ Behind the scenes, there is a single dictionary lookup and a counter increment. Some kinds arguments will take longer to hash, but you get the idea,
@cache has very little overhead.Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
2/ Here are the timings: $ python3.9 -m timeit -r11 -s 'def s(x):pass' 's(5)' 5000000 loops, best of 11: 72.1 nsec per loop $ python3.9 -m timeit -r11 -s 'from functools import cache' -s 'def s(x):pass' -s 'c=cache(s)' 'c(5)' 5000000 loops, best of 11: 60.6 nsec per loop
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
1/
#Python tip: The functools.cache() decorator is astonishingly fast. Even an empty function that returns None can be sped-up by caching it.
https://docs.python.org/3/library/functools.html#functools.cache …Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
Here is the new rendered version with those edits:https://github.com/python/cpython/blob/b049179982f18a86751610b0acce2bab8bdc678f/Doc/howto/descriptor.rst …
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo -
Here's a live draft for the second round of edits. In particular, please look at the new Descriptor Example:https://github.com/python/cpython/pull/22946/files …
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.