Advertisement

Multiple return values like in LUA for a game scripting language

Started by March 14, 2004 09:18 AM
10 comments, last by Jotaf 20 years, 8 months ago
quote: Original post by Zahlman
Oops, I was fixated on the other bit...
We all have our moments

quote: @Kylotan: Can you do something with ''isinstance'' or something similar to determine if a Python object implements this ''sequence'' interface (and sorry for importing language terminology from elsewhere)?
The sequence interface is an abstract concept, but you can use isinstance to determine if an object is of known sequence types since you can pass multiple types to isinstance:
>>> t = (1,2,3)>>> l = [1,2,3]>>> isinstance(t, (tuple,list))True>>> isinstance(l, (tuple,list))True 
Alternatively, you could check the object dictionary for the methods that define the sequence interface. I can''t remember them all right now (and I''m too lazy/busy to look them up for you, sorry), but determining if an object implemented the iterator interface is as simple as checking for the existence of the __iter__ and next methods (the next method is actually a member of the object returned by __iter__, which can be self):
d = dir(file)if ''__iter__'' in d: print ''returns iterator, '',if ''next'' in d: print ''iteratable'' 
Extending that to sequences should be trivial.
Of course, the question generally would be ''why do you need to do this''? Generally you know what you''re passing to a function and doing your own type-checking tends to be a waste of time. If there''s a risk of something going wrong, just be ready to catch the exception (TypeError, in this case).

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

This topic is closed to new replies.

Advertisement