type comparison and wxpython

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • borris

    #1

    type comparison and wxpython

    ive been trying to do a test for type with wxpython objects

    like

    passing in a wx.TextCtrl into

    def XXX(obj)
    if type(obj) is type(self.Butto n)

    I have to make an object self.Button to get its type.
    as I tried is type("wx.Button ") which didnt work.

    trouble is the button must have a parent, which appears on the frame (Ill
    try to get rid of it)

    but I should be able to use text to set the type
    Ive tried "wx._controls.T extCtrl" which didnt work

    any hints welcome

    btw Im newbie to python, coming from Java and I reckon its cool and sleek
    and coming from java Swing, I reckon wxPython is probably better

  • Simon Forman

    #2
    Re: type comparison and wxpython

    borris wrote:
    ive been trying to do a test for type with wxpython objects
    >
    like
    >
    passing in a wx.TextCtrl into
    >
    def XXX(obj)
    if type(obj) is type(self.Butto n)
    >
    I have to make an object self.Button to get its type.
    as I tried is type("wx.Button ") which didnt work.
    type("wx.Button ") would return <type 'str'>, did you mean
    type(wx.Button) ? That still wouldn't be what you wanted.. For
    "classic" classes type() returns <type 'classobj'>; for new style
    classes it returns <type 'type'>


    Assuming that wx.Button is the class of your Button objects then what
    you want is the isinstance() function.

    if isinstance(obj, wx.Button):

    >>help(isinstan ce)
    Help on built-in function isinstance in module __builtin__:

    isinstance(...)
    isinstance(obje ct, class-or-type-or-tuple) -bool

    Return whether an object is an instance of a class or of a subclass
    thereof.
    With a type as second argument, return whether that is the object's
    type.
    The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
    isinstance(x, A) or isinstance(x, B) or ... (etc.).



    >
    trouble is the button must have a parent, which appears on the frame (Ill
    try to get rid of it)
    >
    but I should be able to use text to set the type
    Ive tried "wx._controls.T extCtrl" which didnt work
    huh?
    >
    any hints welcome
    >
    btw Im newbie to python, coming from Java and I reckon its cool and sleek
    and coming from java Swing, I reckon wxPython is probably better

    Comment

    Working...