[portland] __gitattrib__ idoms. How often are they used?
Christopher Hiller
chiller at decipherinc.com
Mon Jan 27 00:18:02 CET 2014
Dunno about whether or not these are "evil" (they are certainly of dubious value), but here's a few use cases:
class Foo(dict):
"""
I am some sort of struct-like object.
This will save you at least one keystroke.
"""
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
self[name] = value
class Bar(object):
import zlib
from codecs import encode
"""
I like to wrap things! Wrapping things is cool.
"""
def __init__(self, value):
self.value = value
def __getattr__(self, name):
return zlib.compress(encode('rot13', str(self.value)))
class Bar(object):
"""
"""
def __init__(self, stuff):
"""
:type stuff: dict
"""
self.stuff = stuff
def __getattr__(self, name):
try:
return Bar(self.stuff[name])
except KeyError:
raise AttributeError(name)
class Baz(object):
"""
I namespace my variables because reasons.
"""
baz_sheep = 'mutton'
baz_cow = 'beef'
baz_pig = 'pork'
def __getattr__(self, name):
return getattr(self, 'baz_%s' % name)
--
Christopher Hiller
Decipher, Inc.
On January 26, 2014 at 2:04:44 PM, mark gross (markgross at thegnar.org) wrote:
On Sun, Jan 26, 2014 at 12:16:37PM -0800, Ethan Furman wrote:
> On 01/26/2014 12:06 PM, mark gross wrote:
> >
> >But, I'd like to ask the pdx-python crew about uses of __getattrib__.
>
> There is no __getattrib__ -- there is a __getattr__ and a __getattribute__; which did you mean?
Sorry for the type-oh. I'm talking about __getattr__
--mark
_______________________________________________
Portland mailing list
Portland at python.org
https://mail.python.org/mailman/listinfo/portland
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/portland/attachments/20140126/92dbca79/attachment.html>
More information about the Portland
mailing list