check type when assignment

P

pipehappy

Hello everyone:

Is there a way to check the type when do assignment?

if I write:
ab = bc
and want to make sure the return value of isinstance(bc, klass) is True
or I will raise
a exception.

Any suggestion?
 
T

Tal Einat

pipehappy said:
Hello everyone:

Is there a way to check the type when do assignment?

if I write:
ab = bc
and want to make sure the return value of isinstance(bc, klass) is True
or I will raise
a exception.

Any suggestion?

1. Check your condition before the assignment: (IMO one-liners are over-rated)

if not isinstance(bc, klass):
raise TypeError # or do whatever else is appropriate
ab = bc

2. If you really insist on doing this in a single statement (in the assignment
itself), write a function for it:

def assert_type(obj, klass):
if not isinstance(bc, klass):
raise TypeError
return obj
ab = assert_type(bc, klass)

Or to be more generic:

def assert_return(obj, func):
assert func(obj)
return obj

ab = assert_return(bc, lambda obj:isinstance(obj, klass))

- Tal
 
D

Diez B. Roggisch

pipehappy said:
Hello everyone:

Is there a way to check the type when do assignment?

if I write:
ab = bc
and want to make sure the return value of isinstance(bc, klass) is True
or I will raise
a exception.

In general, not doable. The assignment operator is not overloadable.

Only if you use assignments of the form

a.foo = bar

you could overwrite the __setattribute__-method to achieve what you want.


Diez
 
B

Bruno Desthuilliers

Diez said:
pipehappy wrote:




In general, not doable. The assignment operator is not overloadable.

Only if you use assignments of the form

a.foo = bar

you could overwrite the __setattribute__-method

(Or use a Descriptor)
 
N

Nick Vatamaniuc

pipe,

In general it is not possible in one line. You have to do it before
hand or after with an if statement.

As a sidenote though you probably shouldn't use isinstance(), you might
need it less than you think you do, especially if you are using it to
check for some interface. For example, do you really care if bc is of
_type_ klass or just that bc just implements method x, y, z and has
arguments a, b,c? The two things seem to be the same, because they are
the same in C++ or Java, but they are not the same in Python. In Python
an interface and type inheritance can be orthogonal to each other. One
could add methods to objects or change them at will after
instantication such that the type hierarchy would be preserved but the
interface would be broken, or alternatively you could have an object of
a different type that fully implements the interface and could easily
be used in the code but just because it doesn't pass the isinstance()
test it is flagged as un-usable.
So in your code you could just test for the interface (methods and/or
attributes) with hasattr(bc, 'method') or even better just call the
method and see what happens, if it doesn't exist it will raise an
exception.

A much better explanation about the use and abuse of isinstance() is
here:
http://www.canonical.org/~kragen/isinstance/

Hope this helps,
Nick V.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top