0

I have an problem, I have two different objects in the same array, they have some arguments in common, so I can access them having no problem.

Now I want to know how can I auto-detect what object I'm accessing to in order to make further actions.

if len(theArray) > 0:
  sol = []
  for ea in elArray:
    ...
    if ea is Type1:
      ...
    elif ea is Type2:
      ...
    else:
      ...

Thanks

2
  • So? Doesn't it work that way? Commented Nov 19, 2013 at 15:06
  • I want to know if this works, haven't tryed because I don't know if it's even possible Commented Nov 19, 2013 at 15:07

1 Answer 1

3

You're confusing the is operator with the isinstance function:

class Foo: pass
f = Foo()
f is Foo # False
isinstance(f,Foo) # True

is, in Python, means "these two objects are the same". isinstance means "does this object inherit from this class".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.