[Q] Type checking...

Klaus Alexander Seistrup kas at maps.magnetic-ink.dk
Mon Jul 19 07:32:58 EDT 1999


Olivier Deckmyn <olivier.deckmyn at mail.dotcom.fr> wrote:

> Abstract : [Q]How to check the type of an instance before(after?)
> sending it to a method...

One possible solution is:

=== snip ===

MAGIC="I'm in class A :-)"

class ClassA:
  def __init__(self):
    self.magic = MAGIC
      :
  # end def __init__
# end class Class A

def is_class_a(obj):
  return has_attr(obj,'magic') and get_attr(obbj,'magic') == MAGIC
# end def is_class_a

class ClassB:
  def myMethod(self,anInstance):
    if not is_class_a (anInstance):
      raise TypeError, ...
        :
    # end if
  # end def myMethod
# end class ClassB

=== snip ===


  //Klaus

-- 
···[ Magnetic Ink ]·················································· ><> ···
···[ http://www.magnetic-ink.dk/ ]···········································




More information about the Python-list mailing list