Jump to content

  • Log In with Google      Sign In   
  • Create Account

Checking list for class instance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Munchkin9   Members   -  Reputation: 125

Like
0Likes
Like

Posted 06 March 2012 - 04:54 PM

In python:

Is there a way to check for an instance (any instance) of a specific class in a list?


Thank you,
Munchkin9

Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
1Likes
Like

Posted 06 March 2012 - 05:24 PM

for element in thelist:
	if isinstance(element, TheClass):
		print 'found an instance'

The usual warnings apply; you shouldn't typically have to care about the exact type of an object, only the behaviours it supports. Consider your design carefully.

#3 Munchkin9   Members   -  Reputation: 125

Like
0Likes
Like

Posted 06 March 2012 - 06:40 PM

Thank you, that is a bit more complicated than I would have liked but I'm not really surprised about that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS