Python seems disallow you to assign attribute to the internal highest level of 'object':
object.lst=lambda o:list(o)
Or
func=lambda o:list(o)
setattr(object,'lst',func)
All generate error messages.
Any design reason behind it? Honestly, this is odd, since everything is an object, and you can define func(obj)
as you wish, so if func is not constraint the argument type, why the method way is prohibited to add then?
obj.func()
is exactly func(obj) if you assign the func as an attribute to the object
. Since everything is an object,an attribute of the object is a global function almost by definition.
Anyway to walk-around?
Basically if you have the capability to add some functions to build-in types, you can do pipeline style programming:obj.list().len().range()
and I think the readibility of this type program is very clear and nothing non-pythonic.