test mult vars to same value, how to shorten expr?

N

notnorwegian

if i want o test:
if a == 5 and b ==5 and c==5 ... z==5

is there some synctactic suagr for this?

rather than maiking one of my own i mean, something built-in like:
if a,b,c... z == 5:
 
M

Marc 'BlackJack' Rintsch

if i want o test:
if a == 5 and b ==5 and c==5 ... z==5

is there some synctactic suagr for this?

rather than maiking one of my own i mean, something built-in like:
if a,b,c... z == 5:

Since Python 2.5 there's `all()`:

In [81]: a, b, c, d = 5, 5, 5, 6

In [82]: all(x == 5 for x in (a, b, c))
Out[82]: True

In [83]: all(x == 5 for x in (a, b, c, d))
Out[83]: False

Ciao,
Marc 'BlackJack' Rintsch
 
P

Peter Otten

if i want o test:
if a == 5 and b ==5 and c==5 ... z==5

is there some synctactic suagr for this?

rather than maiking one of my own i mean, something built-in like:
if a,b,c... z == 5:

if all(x == 5 for x in a,b,c,...):
print "yep"


Peter
 
C

castironpi

 if i want o test:
if a == 5 and b ==5 and c==5 ... z==5
is there some synctactic suagr for this?
rather than maiking one of my own i mean, something built-in like:
if a,b,c... z == 5:

if all(x == 5 for x in a,b,c,...):
   print "yep"

Peter
a,b,c,d=5,5,5,5
import functools
import operator
myequals= functools.partial( operator.eq, 5 )
map( myequals, ( a, b, c, d ) ) [True, True, True, True]
all( _ )
True
 

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
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top