Can't return a value for __getitem__

Anonymous avatarAnonymous created an issue

It doesn't seem possible to have a return element for getitem. For example:

from dingus import Dingus

d = Dingus(__getitem____returns='foo')
print d['bar'] # expects 'foo' but you get the initial dingus

Another option is to try this:

d = Dingus(**{"['bar']__returns": 'foo'})

This ends up adding a getitem call to the call list.

Comments (2)

  1. Gary Bernhardt
    • changed status to open

    Wow, good catch. This is because getitem is an actual method, not just another dingus like most attributes. I'm at a bit of a loss about how to make this work without introducing a ton of internally confusing complexity. I think that the getattr function would have to return a special dingus that, when called, logs the special method name in the parent *and* does the normal getattr stuff.

    That last bit is important: dinguses act as perfectly valid dictionaries, and that needs to remain true.

    Fortunately, that means you can temporarily work around your problem by doing:

    >>> d = Dingus()
    >>> d['foo'] = 'bar'
    >>> print d['foo']

    You could also monkey patch a new getattr onto the dingus if you really need it to return a specific value regardless of how it's called. Ugly, I know, but I'm definitely not going to have time to fix this soon. :( Clever patches are encouraged, of course! :)

  2. Gary Bernhardt

    Actually, if you need a dingus to return a value regardless of the getitem key, a better short-term solution would be to just subclass Dingus and override getitem to return the value.

    My allergy to inheritance sometimes keeps me from seeing it when it really is the solution you want! ;)

  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.