3 ms·
The best use of Python Classes is as a stand-in for Javascript-style object syntax, ala: class foo: pass f = foo() f.bar = "yes" which is easier
by danbmil99 15y ago
The best use of Python Classes is as a stand-in for Javascript-style object syntax, ala:
class foo:
pass
f = foo()
f.bar = "yes"
which is easier to type and more readable than:
f = {}
f['bar'] = "yes"
(credit where due: learned this trick from TB)
- hboon 15y agoWho or what is TB?
- stock_toaster 15y agoYou can simplify it a bit by building the container class on the fly, if desired. >>> x = type('Foo', (object,), {}) >>> x.bar = 1 >>> x.bar 1