Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Saturday, October 23, 2010

How to test if object is sequence, or iterable?

In the java or c# languages, we can found that it is very easy to know that class is a collection by checking the interface Collection. But in python some time it can't check by using isinstance, issubclass methods. So, the solution is we can just used the __iter__ attribute in that object.
we can use hasattr method to check it and it will return boolean which it has that attr or not.

"hasattr(object,'__iter__')"

x = list()
print(hasattr(x,'__iter__'))

Now, you can check object is iterable or not. Hope that it will be useful.

No comments:

Post a Comment