Isinstance not supported, workaround?

24 views
Skip to first unread message

sal

unread,
Jul 22, 2013, 8:32:28 PM7/22/13
to shedskin...@googlegroups.com

Since isinstance isn't supported (if I'm understanding right), what is the recommended alternative?  Or should I 'roll my own' type detector?  Anyone have any workarounds / suggestions?

John Yeung

unread,
Jul 22, 2013, 9:02:49 PM7/22/13
to shedskin...@googlegroups.com
If your code is dependent on isinstance, my suggestion is to think
less dynamically. ;)

The point of ShedSkin is to take code which uses types that it can
figure out in advance (just by analyzing the source, without running
it), and turn that into C++. The major speed gains come from taking
advantage of what is in effect static typing. If you need to test
types at run-time, chances are your code is not a good fit for
ShedSkin.

But in many cases, you can just rewrite your code to so that you don't
reuse names for multiple types. So, try thinking of your problem in
statically typed terms.

John

sal

unread,
Jul 23, 2013, 5:32:53 PM7/23/13
to shedskin...@googlegroups.com
Looks like x.__class__ == y.__class__ works.  Good enough here =)

No typecasting though, so agreed, logic probably just needs to be rewritten. Hmm.

Was looking to do:

if (x isa Duck) x.quack() else x.bark()

Type of thing.  Guess its just x.makeNoise() with overridden methods then... but before going that route, thought I was missing something obvious.  I think with pypy, there is a typecast workaround for the scenario.

THanks for tips :)

John Yeung

unread,
Jul 23, 2013, 6:07:56 PM7/23/13
to shedskin...@googlegroups.com
On Tue, Jul 23, 2013 at 5:32 PM, sal <svf...@gmail.com> wrote:

> I think with pypy, there is a typecast workaround for the scenario.

PyPy shouldn't need any kind of workaround. It is a complete
implementation of Python 2.7.3. Unlike ShedSkin, PyPy offers the full
dynamic capabilities of Python. The speed improvements provided by
PyPy are not as extreme as those provided by ShedSkin.

John

sal

unread,
Jul 23, 2013, 11:09:15 PM7/23/13
to shedskin...@googlegroups.com
Pypy's rpython compiler has ability to up/downcast via an 'assert isinstance'. I was wondering if shedskin had something similair. It's very useful for heavy OO code.

There was some previous discussion here: https://groups.google.com/forum/m/#!topic/shedskin-discuss/CkhPnB555Qs

I would 'try' to hack something like this in, but too unfamiliar with shedskin internals yet. Hmm, need some creative thoughts on how to bribe srepmub... ;-)

Reply all
Reply to author
Forward
0 new messages