[Ironpython-users] Calling len() on a DynamicObject instance?
jdhardy at gmail.com
Thu Oct 10 18:36:49 CEST 2013
On Thu, Oct 10, 2013 at 8:59 AM, Nicholas Devenish <misnomer at gmail.com> wrote:
> Hi All,
> I have a DynamicObject subclass that is being passed into IronPython. Sometimes, this subclass represents a collection - at which point it is currently either Enumerated through TryConvert or Indexed by TryGetIndex.
> However, when in python code calling len(myobject), I get the error "len() of unsized object" - And I can't work out what it is doing such that I can get this to work - I thought the TryUnaryOperation overload might be involved, but it doesn't seem to get called at all.
> Is there a way to achieve this?
I haven't looked at the implementation of len() yet, so I could be
wrong, but my guess is that if your object doesn't implement IList (or
something else with .Count) it will look for a __len__ method
(http://bit.ly/16TDkIJ) on the object. This should be reified as a
call to TryGetMember/TryInvoke, so you should be able to selectively
provide __len__ for those objects that are collections and leave it
out for those that are not.
More information about the Ironpython-users