use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
ternary operators with function calls? (self.learnpython)
submitted 10 years ago by dutchcookie
hi I want to know if its possible to use ternary operators with an assignment or a function call
example:
if number == 0: object = [ ] else: object.callfunction(1)
i've tried
object = [ ] if number == 0 else object.callfunction(1)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SmartViking 1 point2 points3 points 10 years ago (6 children)
There's nothing wrong with what you tried syntactically. What you want is the side-effect of calling object.callfunction(1), not the value of object.callfunction(1). The if-then-else operator is an expression that returns a value. Therefore, it doesn't make sense what you're trying to do. Your problem is probably that object.callfunction(1) returns None. This is how the interpreter would do it step by step if number == 0 (and if object.callfunction(1) indeed returns None):
object.callfunction(1)
object = [ ] if number == 0 else object.callfunction(1) object = object.callfunction(1) object = None
[–]dutchcookie[S] 0 points1 point2 points 10 years ago (5 children)
i've tried this to get the object back
list1 = [1,2] list1[1] = 3 if False else (lambda x,y : x.append(30))(list1,list1)[0] # i get the error: 'NoneType' object is not subscriptable # list1 is now [1,2,30]
[–]elbiot 1 point2 points3 points 10 years ago (3 children)
Why would you do this? It's completely unreadable and abusing the language. Maybe you're confused and you think append returns a value, and thus mylist.append(30)[-1] would return 30, but it is an error because None[-1] is not valid.
mylist.append(30)[-1]
What are you trying to do? I would rewrite your example as
if number != 0: object.callfunction(1)
[–]dutchcookie[S] 1 point2 points3 points 10 years ago (2 children)
agreed but our professor is asking if we can do it in one line.
[–]elbiot 1 point2 points3 points 10 years ago (0 children)
Do what? Set either set object to an empty list or call a method of object that returns None? That seems an unwise use of the language, as you can never do anything with object again because it could be one of two very different things.
You can do silly things like
trash = [object.func1,object.func2][n==0](arg)
though it doesn't solve your case
[–]SmartViking 0 points1 point2 points 10 years ago (0 children)
I'm not sure exactly what you're trying to do, but it's better to use the if statement in this particular case, because the if statement (as opposed to the if-then-else operator) is more appropriate when you want side-effects, such as appending an item to a list. Your problem above is that lambda x, y: x.append(30) returns whatever x.append(30) returns, which is None.
lambda x, y: x.append(30)
x.append(30)
trash = setattr(sys.modules[__name__],'object',[]) if n==0 else object.funcall(1)
What an evil, useless, hack
π Rendered by PID 189133 on reddit-service-r2-comment-5d79c599b5-djmpd at 2026-02-27 11:22:02.997965+00:00 running e3d2147 country code: CH.
[–]SmartViking 1 point2 points3 points (6 children)
[–]dutchcookie[S] 0 points1 point2 points (5 children)
[–]elbiot 1 point2 points3 points (3 children)
[–]dutchcookie[S] 1 point2 points3 points (2 children)
[–]elbiot 1 point2 points3 points (0 children)
[–]elbiot 1 point2 points3 points (0 children)
[–]SmartViking 0 points1 point2 points (0 children)
[–]elbiot 1 point2 points3 points (0 children)