Dict-like collection for attributes - python

Is there a class in Python 2.7 that behaves like:
x = AttrDict(foo=1, bar=2)
x.foo -> 1
x.bar -> 2
for arbitrary attributes / constructor keywords?
A dict is close, but doesn't have attribute access nor such a nice constructor syntax.
A named tuple comes close too, but I don't want to create a new type for each combination of attributes.
Something like that would be handy to have sometimes. I could make one myself in a minute, but it sounds like something that might exist in Python already.

In Python 3.3, the standard solution is types.SimpleNamespace, but this is not backported to 2.7. A very similar type, argparse.Namespace exists in 2.7. It's a little obscure but documented and hence okay to rely on.

Going along with the subclassing solution, you could do something like this:
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
It looks a little weird, but it should work. It takes advantage of the builtin attribute location for python objects __dict__
>>>x = AttrDict(foo=1, bar=2)
>>>x.foo
1
>>>x.bar
2
>>>x.baz = 3
>>>x.baz
3
>>>x.missing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'AttrDict' object has no attribute 'missing'

You can subclass dict to do something like this:(Thanks to #delnan, two such objects are already available in standard library)
class AttrDict(dict):
def __getattr__(self, attr):
return self[attr]
def __setattr__(self, attr, val):
self.__setitem__(attr, val)
...
>>> x = AttrDict(foo=1, bar=2)
>>> x.foo
1
>>> x.bar
2
>>> x.spam = 'eggs'
>>> x.spam
'eggs'

Related

How do I add values to parameters in namedtuple() using a loop? [duplicate]

Suppose I have a python object x and a string s, how do I set the attribute s on x? So:
>>> x = SomeObject()
>>> attr = 'myAttr'
>>> # magic goes here
>>> x.myAttr
'magic'
What's the magic? The goal of this, incidentally, is to cache calls to x.__getattr__().
setattr(x, attr, 'magic')
For help on it:
>>> help(setattr)
Help on built-in function setattr in module __builtin__:
setattr(...)
setattr(object, name, value)
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to
``x.y = v''.
However, you should note that you can't do that to a "pure" instance of object. But it is likely you have a simple subclass of object where it will work fine. I would strongly urge the O.P. to never make instances of object like that.
Usually, we define classes for this.
class XClass( object ):
def __init__( self ):
self.myAttr= None
x= XClass()
x.myAttr= 'magic'
x.myAttr
However, you can, to an extent, do this with the setattr and getattr built-in functions. However, they don't work on instances of object directly.
>>> a= object()
>>> setattr( a, 'hi', 'mom' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'hi'
They do, however, work on all kinds of simple classes.
class YClass( object ):
pass
y= YClass()
setattr( y, 'myAttr', 'magic' )
y.myAttr
let x be an object then you can do it two ways
x.attr_name = s
setattr(x, 'attr_name', s)
Also works fine within a class:
def update_property(self, property, value):
setattr(self, property, value)
If you want a filename from an argument:
import sys
filename = sys.argv[1]
file = open(filename, 'r')
contents = file.read()
If you want an argument to show on your terminal (using print()):
import sys
arg = sys.argv[1]
arg1config = print(arg1config)

Optional chaining for Python objects: foo?.bar?.baz

In JavaScript, if I'm not sure whether every element of the chain exists/is not undefined, I can do foo?.bar, and if bar does not exist on foo, the interpreter will silently short circuit it and not throw an error.
Is there anything similar in Python? For now, I've been doing it like this:
if foo and foo.bar and foo.bar.baz:
# do something
My intuition tells me that this isn't the best way to check whether every element of the chain exists. Is there a more elegant/Pythonic way to do this?
If it's a dictionary you can use get(keyname, value)
{'foo': {'bar': 'baz'}}.get('foo', {}).get('bar')
Most pythonic way is:
try:
# do something
...
except (NameError, AttributeError) as e:
# do something else
...
You can use getattr:
getattr(getattr(foo, 'bar', None), 'baz', None)
You can use the Glom.
from glom import glom
target = {'a': {'b': {'c': 'd'}}}
glom(target, 'a.b.c', default=None) # returns 'd'
https://github.com/mahmoud/glom
I like modern languages like Kotlin which allow this:
foo?.bar?.baz
Recently I had fun trying to implement something similar in python:
https://gist.github.com/karbachinsky/cc5164b77b09170edce7e67e57f1636c
Unfortunately, the question mark is not a valid symbol in attribute names in python, thus I used a similar mark from Unicode :)
Combining a few things I see here.
from functools import reduce
def optional_chain(obj, keys):
try:
return reduce(getattr, keys.split('.'), obj)
except AttributeError:
return None
optional_chain(foo, 'bar.baz')
Or instead extend getattr so you can also use it as a drop-in replacement for getattr
from functools import reduce
def rgetattr(obj, attr, *args):
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return reduce(_getattr, attr.split('.'), obj)
With rgetattr it can still raise an AttributeError if the path does not exist, and you can specify your own default instead of None.
Combining some of the other answers into a function gives us something that's easily readable and something that can be used with objects and dictionaries.
def optional_chain(root, *keys):
result = root
for k in keys:
if isinstance(result, dict):
result = result.get(k, None)
else:
result = getattr(result, k, None)
if result is None:
break
return result
Using this function you'd just add the keys/attributes after the first argument.
obj = {'a': {'b': {'c': {'d': 1}}}}
print(optional_chain(obj, 'a', 'b'), optional_chain(obj, 'a', 'z'))
Gives us:
{'c': {'d': 1}} None
Classes can override __getattr__ to return a default value for missing attributes:
class Example:
def __getattr__(self, attr): # only called when missing
return None
Testing it:
>>> ex = Example()
>>> ex.attr = 1
>>> ex.attr
1
>>> ex.missing # evaluates to `None
>>>
However, this will not allow for chaining:
>>> ex.missing.missing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'missing'
Nor will it deal with attempts to call methods that are absent:
>>> ex.impossible()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
To fix this, we can make a proxy object:
class GetAnything:
def __getattr__(self, attr):
return self
def __call__(self, *args, **kwargs): # also allow calls to work
return self
def __repr__(self):
return '<Missing value>'
# Reassign the name to avoid making more instances
GetAnything = GetAnything()
And return that instead of None:
class Example:
def __getattr__(self, attr): # only called when missing
return GetAnything
Now it chains as desired:
>>> Example().missing_attribute.missing_method().whatever
<Missing value>
Here's some syntactic sugar to make chaining with getattr look more like the fluent interfaces of other languages. It's definitely not "Pythonic", but it allows for something simpler to write.
The idea is to abuse the # operator added in Python 3.5 (to support matrix multiplication in Numpy). We define a class r such that its instances, when matrix-multiplied on the right of another object, invoke getattr. (The combination #r, of course, is read "attr".)
class r:
def __init__(self, name, value=None):
self._name = name
self._value = value
def __rmatmul__(self, obj):
return getattr(obj, self._name, self._value)
Now we can chain attribute accesses easily, without having to modify any other classes (and of course it works on built-in types):
>>> 'foo'#r('bar')#r('baz') # None
>>>
However, the order of operations is inconvenient with method calls:
>>> 'foo bar'#r('split')()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'r' object is not callable
>>> ('foo bar'#r('split'))()
['foo', 'bar']
Python 3.10 introduced the match statement in PEP-634, with the tutorial in PEP-636 being a nice reference.
This statement allow these sorts of "chained" operations to be performed, but note that they are statements and not expressions.
For example, OP could instead do:
match foo:
case object(bar=object(baz=baz)) if baz:
# do something with baz
The reason for needing object is that everything is a subtype of it and hence it always succeeds. It then goes on to check that the attribute exists, which might fail. Exceptions wouldn't be thrown if the attribute didn't exist, just the case wouldn't match and it would move onto the next one (which in this case doesn't exist, so nothing would be done).
A more realistic example would check something more specific, e.g.:
from collections import namedtuple
Foo = namedtuple('Foo', ['bar'])
Bar = namedtuple('Bar', ['baz'])
def fn(x):
match x:
case Foo(bar=Bar(baz=baz)):
return baz
print(fn(Foo(bar=Bar(baz='the value'))))
print(fn(None))
print(fn(1))
which would output:
the value
None
None
If instead you wanted to destructure into dictionaries, you might use something like:
foo = {'bar': {'baz': 'the value'}}
match foo:
case {'bar': {'baz': baz}}:
print(baz)

What really makes an object callable in python [duplicate]

I would like to do the following:
class A(object): pass
a = A()
a.__int__ = lambda self: 3
i = int(a)
Unfortunately, this throws:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'A'
This only seems to work if I assign the "special" method to the class A instead of an instance of it. Is there any recourse?
One way I thought of was:
def __int__(self):
# No infinite loop
if type(self).__int__.im_func != self.__int__.im_func:
return self.__int__()
raise NotImplementedError()
But that looks rather ugly.
Thanks.
Python always looks up special methods on the class, not the instance (except in the old, aka "legacy", kind of classes -- they're deprecated and have gone away in Python 3, because of the quirky semantics that mostly comes from looking up special methods on the instance, so you really don't want to use them, believe me!-).
To make a special class whose instances can have special methods independent from each other, you need to give each instance its own class -- then you can assign special methods on the instance's (individual) class without affecting other instances, and live happily ever after. If you want to make it look like you're assigning to an attribute the instance, while actually assigning to an attribute of the individualized per-instance class, you can get that with a special __setattr__ implementation, of course.
Here's the simple case, with explicit "assign to class" syntax:
>>> class Individualist(object):
... def __init__(self):
... self.__class__ = type('GottaBeMe', (self.__class__, object), {})
...
>>> a = Individualist()
>>> b = Individualist()
>>> a.__class__.__int__ = lambda self: 23
>>> b.__class__.__int__ = lambda self: 42
>>> int(a)
23
>>> int(b)
42
>>>
and here's the fancy version, where you "make it look like" you're assigning the special method as an instance attribute (while behind the scene it still goes to the class of course):
>>> class Sophisticated(Individualist):
... def __setattr__(self, n, v):
... if n[:2]=='__' and n[-2:]=='__' and n!='__class__':
... setattr(self.__class__, n, v)
... else:
... object.__setattr__(self, n, v)
...
>>> c = Sophisticated()
>>> d = Sophisticated()
>>> c.__int__ = lambda self: 54
>>> d.__int__ = lambda self: 88
>>> int(c)
54
>>> int(d)
88
The only recourse that works for new-style classes is to have a method on the class that calls the attribute on the instance (if it exists):
class A(object):
def __int__(self):
if '__int__' in self.__dict__:
return self.__int__()
raise ValueError
a = A()
a.__int__ = lambda: 3
int(a)
Note that a.__int__ will not be a method (only functions that are attributes of the class will become methods) so self is not passed implicitly.
I have nothing to add about the specifics of overriding __int__. But I noticed one thing about your sample that bears discussing.
When you manually assign new methods to an object, "self" is not automatically passed in. I've modified your sample code to make my point clearer:
class A(object): pass
a = A()
a.foo = lambda self: 3
a.foo()
If you run this code, it throws an exception because you passed in 0 arguments to "foo" and 1 is required. If you remove the "self" it works fine.
Python only automatically prepends "self" to the arguments if it had to look up the method in the class of the object and the function it found is a "normal" function. (Examples of "abnormal" functions: class methods, callable objects, bound method objects.) If you stick callables in to the object itself they won't automatically get "self".
If you want self there, use a closure.

Equivalent of MATLAB variable field names in Python [duplicate]

Suppose I have a python object x and a string s, how do I set the attribute s on x? So:
>>> x = SomeObject()
>>> attr = 'myAttr'
>>> # magic goes here
>>> x.myAttr
'magic'
What's the magic? The goal of this, incidentally, is to cache calls to x.__getattr__().
setattr(x, attr, 'magic')
For help on it:
>>> help(setattr)
Help on built-in function setattr in module __builtin__:
setattr(...)
setattr(object, name, value)
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to
``x.y = v''.
However, you should note that you can't do that to a "pure" instance of object. But it is likely you have a simple subclass of object where it will work fine. I would strongly urge the O.P. to never make instances of object like that.
Usually, we define classes for this.
class XClass( object ):
def __init__( self ):
self.myAttr= None
x= XClass()
x.myAttr= 'magic'
x.myAttr
However, you can, to an extent, do this with the setattr and getattr built-in functions. However, they don't work on instances of object directly.
>>> a= object()
>>> setattr( a, 'hi', 'mom' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'hi'
They do, however, work on all kinds of simple classes.
class YClass( object ):
pass
y= YClass()
setattr( y, 'myAttr', 'magic' )
y.myAttr
let x be an object then you can do it two ways
x.attr_name = s
setattr(x, 'attr_name', s)
Also works fine within a class:
def update_property(self, property, value):
setattr(self, property, value)
If you want a filename from an argument:
import sys
filename = sys.argv[1]
file = open(filename, 'r')
contents = file.read()
If you want an argument to show on your terminal (using print()):
import sys
arg = sys.argv[1]
arg1config = print(arg1config)

Best way to implement/call a class that returns an immutable value?

I would something like this in Python:
result = SomeClass(some_argument)
Here is the catch though. I don't want the result to be an instance but an immutable object (int, for example). Basically the hole role of a class is returning a value calculated from the argument. I am using a class and not a function for DRY purposes.
Since the above code won't work because it will always return an instance of SomeClass what would be the best alternative?
My only idea is to have a static method, but I don't like it:
result = SomeClass.static_method(some_argument)
You can override __new__. This is rarely a good idea and/or necessary though ...
>>> class Foo(object):
... def __new__(cls):
... return 1
...
>>> Foo()
1
>>> type(Foo())
<type 'int'>
If you don't return an instance of cls, __init__ will never be called.
Basically class methods are the way to go if you have a factory method.
About the result - it really depends on what kind of immutability you seek, but basically namedtuple does a great job for encapsulating things and is also immutable (like normal tuples):
from collections import namedtuple
class FactoryClass(object):
_result_type = namedtuple('ProductClass', ['prod', 'sum'])
#classmethod
def make_object(cls, arg1, arg2):
return cls._result_type(prod=arg1 * arg2, sum=arg1 + arg2)
>>> FactoryClass.make_object(2,3)
ProductClass(prod=6, sum=5)
>>> x = _
>>> x.prod = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

Categories