what is the the best way to express following:

A

AndyL

if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()


In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.
 
A

AndyL

James said:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()


Is not that a shortcut? I have got "KeyError" exception ...
 
A

AndyL

Paul said:
AndyL said:
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()


In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.


Your literal Pythonic approach is to use the isinstance() builtin:

if isinstance(key,(int,long)):
abc()
elif isinstance(key,str):
efg():
elif isinstance(key,(tuple,list)):
ijk()

thx a lot . In fact I do not do dispatch, just wanted ilustrate if:
elif: sequence. Andy
 
P

Paul McGuire

AndyL said:
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()


In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.

Your literal Pythonic approach is to use the isinstance() builtin:

if isinstance(key,(int,long)):
abc()
elif isinstance(key,str):
efg():
elif isinstance(key,(tuple,list)):
ijk()


But since you are dispatching to a method based on type (or some other type
of enumerated condition), even more Pythonic is to use a dict to create a
dispatch table:

dispatch = {
int : abc,
long : abc,
str : efg,
tuple : ijk,
list : ijk,
}
try:
fn = dispatch[ type(key) ]
except KeyError:
print "No handler for key of type", type(key)
else:
fn()


-- Paul
 
J

James Stroud

AndyL said:
if type(key).__name__ == "int" or type(key).__name__ == "long":
abc()
elif type(key).__name__ == "str":
efg()
elif type(key).__name__ == "tuple" or type(key).__name__ == "list":
ijk()


In other words I need to determinie intiger type or string or []/() in
elegant way, possibly without or ...

Thx, A.

Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

James Stroud

AndyL said:
James said:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()



Is not that a shortcut? I have got "KeyError" exception ...

No, I hit send too soon, mixing ideas in my head. Paul McGuire's answer
is the way to go.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
F

Felipe Almeida Lessa

Em Seg, 2006-03-27 às 18:43 -0800, James Stroud escreveu:
Here is a suggestion

todo = {(int, long):abc, (str,):afg, (tuple, list):ijk}
todo[type(key)]()

Maybe...

todo = {(int, long): abc, basestr: afg, (tuple, list): ijk}
(y for x,y in todo.iteritems() if isinstance(key, x)).next()()

Forget it! Damn, that's too unpythonic! (or perlish =)
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top