By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,443 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,443 IT Pros & Developers. It's quick & easy.

check type when assignment

P: n/a
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?

Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
pipehappy <pipehappy <atgmail.comwrites:
>
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

Jul 12 '06 #2

P: n/a
pipehappy wrote:
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
Jul 12 '06 #3

P: n/a
Diez B. Roggisch wrote:
pipehappy wrote:

>>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
(Or use a Descriptor)
to achieve what you want.
>
Diez

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 12 '06 #4

P: n/a
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.

pipehappy wrote:
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?
Jul 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.