1,105,295 Community Members

Conditional assignments

Member Avatar
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was wondering, is it possible to do something like the following? The given code obviously doesn't work.

l = 5

(l == 5)*(k = 7) #Returns error message.

The reason is, I'd rather avoid doing something like the following for the project I'm currently working on.

if stmt1 == True or stmt2 == True:


  if stmt1 == True:

    k = 7

  if k == 7:

    # Evaluate suite

  else:

    # Evaluate different suite
Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

No it's not possible to have assignments in if conditions. The reason is that assignment is a statement and cannot be part of an expression (the same applies to += -=, etc), while the 'if' statement expects an expression. The potential problem with your code above is that it evaluates stmt1 several times. You could write it this way

c = expr1
if c or expr2:
    if c:
        k = 7
    if k == 7:
        # etc
    else:
        # etc

Notice that '== True' is usually not needed. I know it looks restricting if you're used to C code, but it is one of the features that make python code clear.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Something along these lines?

>>> ctrl = 5
>>> k = 7 if ctrl == 5 else None
>>> k
7
Member Avatar
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks fellas, that was helpful. Gribouillis, It turns out that the method you outlined is actually equivalent to what I am trying to do, so I can make use of that. PyTony, I wanted to have the assignment nested in an if statement; I was hoping to avoid having to do precisely what you outlined.

Question Answered as of 2 Years Ago by Gribouillis and pyTony
Member Avatar
Thisisnotanid
Junior Poster in Training
60 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It turns out that something like what's being discussed is possible. It's not equivalent, but if you design with this catch in mind, you can make your programs behave in a way which mimics the desired behavior:

l = 5

k = 7*(l == 5)

Note, that while implementing boolean algebra in such a way can come handy, it can not always replace logic. For example, if '*' in the following is not replaced with 'and', the code will return an error message.

list = range(10) # Some list

a = len(list)

b = a + 5

for l in xrange(b):

  if (l < a)*(not list[l]%2):

    print list[l]
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: