3 ms·
TIL - Enum, auto(), @dataclass, and that classes don't need (object) anymore. Neat!
by iandanforth 7y ago
TIL - Enum, auto(), @dataclass, and that classes don't need (object) anymore. Neat!
- DonaldPShimoda 7y agoEDIT: nope, nope, this is all wrong. My mistake. Ignore everything else here. > classes don't need (object) anymore My friend, old-style classes became unnecessary with the release of Python 2.2 — almost 20 years ago now [0]. I'm afraid you've been living in the past. :) [0] https://wiki.python.org/moin/NewClassVsClassicClass https://wiki.python.org/moin/NewClassVsClassicClass
- Myrmornis 7y agoNope, she/he has not. (object) were the new style classes!
- DonaldPShimoda 7y agoOh man, major brain fart there. You're completely right of course. I was thinking of the transition to classes-as-types in general. Probably would've helped if I actually read the page I linked...
- nerdwaller 7y agoExtending `object` was necessary until 2.7 when it became the default and old style classes were removed entirely in 3. So I think while the GP comment recently learned they don’t need to extend `object` anymore, they are/were familiar with new style vs old style classes.
- DonaldPShimoda 7y agoRight! Yes, another commenter pointed that out to me too. I made a goof and misremembered some things haha. Oops.
- auscompgeek 7y agoNew-style classes never became the default in Python 2: Python 2.7.15 (default, Jun 27 2018, 13:05:28) [GCC 8.1.1 20180531] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... pass ... >>> Foo <class __main__.Foo at 0x6218e384ee88> >>> class Bar(object): ... pass ... >>> Bar <class '__main__.Bar'> >>> isinstance(Bar, type) True >>> isinstance(Foo, type) False
- nerdwaller 7y agoThank you for the correction! I’ve not used Py2 in 6 or so years.
- nerdwaller 7y agoA funky thing about the Enum class is you may also want the `@enum.unique` decorator in most cases, which enforces the enum/value pair being unique. I found the documentation a little misleading until I found that [0]. [0] https://docs.python.org/3/library/enum.html#enum.unique https://docs.python.org/3/library/enum.html#enum.unique
- DonaldPShimoda 7y agoI'm not sure I follow the purpose of this, and the documentation is not terribly elucidating haha. What's a case where I'll have a problem if I don't use this decorator? (I've used the Enum class a bit since it became available, but not in any particularly interesting implementations so maybe that's why I've never heard of this.)
- clawlor 7y ago``@enum.unique`` will raise a ValueError if you accidentally include the same value twice in an enum definition: In [21]: @enum.unique ...: class Animal(enum.Enum): ...: DOG = 1 ...: CAT = 2 ...: PIG = 2 ...: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-21-cbc1625bb41c> in <module> 1 @enum.unique ----> 2 class Animal(enum.Enum): 3 DOG = 1 4 CAT = 2 5 PIG = 2 ~/.virtualenvs/py3/lib/python3.6/enum.py in unique(enumeration) 834 ["%s -> %s" % (alias, name) for (alias, name) in duplicates]) 835 raise ValueError('duplicate values found in %r: %s' % --> 836 (enumeration, alias_details)) 837 return enumeration 838 ValueError: duplicate values found in <enum 'Animal'>: PIG -> CAT It's intended to catch programmer errors - not really useful for small enums like this one, but in a large enum, having duplicates could be difficult to notice, and might cause some insidious bugs.
- DonaldPShimoda 7y ago