isinstance() on wxWindows objects

dear wxPerts,

After searching the archives, I am unable to find a clear explanation of the following.

given:

sizer = wxBoxSizer(wxVERTICAL)

why does

isinstance(sizer, wxSizer) fail,

while

isinstance(sizer, wxBoxSizer) passes?

I thought isinstance() was supposed to 'do the right thing' for derived classes...

Shi.

That inheritance hierarchy only exists in C++. In the Python wrapper
wxPython, it's a bit more convoluted. Use the source Luke! :slight_smile: There
is not so much other documentation I'm afraid.

class wxSizer(wxSizerPtr):

class wxBoxSizerPtr(wxSizerPtr):

class wxBoxSizer(wxBoxSizerPtr):

isinstance(sizer, wxSizerPtr) should do what you want I guess...

···

At 13:23 2002-11-27 -0500, you wrote:

I thought isinstance() was supposed to 'do the right thing' for derived classes...

--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se

thanks, Magnus - that did the trick.. &:>

Shi.

Magnus Lycka wrote:

···

At 13:23 2002-11-27 -0500, you wrote:

>
>> I thought isinstance() was supposed to 'do the right thing' for
>> derived classes...
>
> That inheritance hierarchy only exists in C++. In the Python wrapper
> wxPython, it's a bit more convoluted. Use the source Luke! :slight_smile: There
> is not so much other documentation I'm afraid.
>
> class wxSizer(wxSizerPtr):
>
> class wxBoxSizerPtr(wxSizerPtr):
>
> class wxBoxSizer(wxBoxSizerPtr):
>
> isinstance(sizer, wxSizerPtr) should do what you want I guess...
>

Shi Sherebrin wrote:

dear wxPerts,

After searching the archives, I am unable to find a clear explanation of the following.

given:

sizer = wxBoxSizer(wxVERTICAL)

why does

isinstance(sizer, wxSizer) fail,

while

isinstance(sizer, wxBoxSizer) passes?

I thought isinstance() was supposed to 'do the right thing' for derived classes...

It does, but there are some implementaiton classes that through things off a bit. wxBoxSizer's heirarchy is:

wxObjectPtr
     \
      \
    wxSizerPtr
     / \
    / \
wxSizer wxBoxSizerPtr
              \
               \
             wxBoxSizer

It's a pain but it's just the way this version of SWIG works.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Magnus Lycka wrote:

···

At 13:23 2002-11-27 -0500, you wrote:

I thought isinstance() was supposed to 'do the right thing' for derived classes...

That inheritance hierarchy only exists in C++. In the Python wrapper
wxPython, it's a bit more convoluted. Use the source Luke! :slight_smile: There
is not so much other documentation I'm afraid.

class wxSizer(wxSizerPtr):

class wxBoxSizerPtr(wxSizerPtr):

class wxBoxSizer(wxBoxSizerPtr):

isinstance(sizer, wxSizerPtr) should do what you want I guess...

This would be better:

  isinstance(b, (wxSizer, wxSizerPtr))

just in case I ever "fix" the code generator to use a more consistent (and sane) class heriarchy.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Magnus Lycka wrote:

...

isinstance(sizer, wxSizerPtr) should do what you want I guess...

This would be better:

    isinstance(b, (wxSizer, wxSizerPtr))

just in case I ever "fix" the code generator to use a more consistent (and sane) class heriarchy.

Robin, was that supposed to be pseudocode, or will it only work in Python v2.2+? I'm using v2.1 and I get:

TypeError: isinstance() arg 2 must be a class or type

I also tried a list instead of a tuple and got the same result

I'm planning to do the following:

result_list = map(isinstance, [sizer, sizer], [wxSizer, wxSizerPtr])
result = filter(None, result_list)
assert(result)

but just wanted to check first to see if I was not 'getting' something..

thanks,
Shi.

Shi Sherebrin wrote:

Robin Dunn wrote:

Magnus Lycka wrote:

...

isinstance(sizer, wxSizerPtr) should do what you want I guess...

This would be better:

    isinstance(b, (wxSizer, wxSizerPtr))

just in case I ever "fix" the code generator to use a more consistent (and sane) class heriarchy.

Robin, was that supposed to be pseudocode, or will it only work in Python v2.2+?

Yes, apparently.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!