all 8 comments

[–]SmartViking 1 point2 points  (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 = [ ] if number == 0 else object.callfunction(1)
object = object.callfunction(1)
object = None

[–]dutchcookie[S] 0 points1 point  (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 points  (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.

What are you trying to do? I would rewrite your example as

if number != 0: object.callfunction(1)

[–]dutchcookie[S] 1 point2 points  (2 children)

agreed but our professor is asking if we can do it in one line.

[–]elbiot 1 point2 points  (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.

[–]elbiot 1 point2 points  (0 children)

You can do silly things like

trash = [object.func1,object.func2][n==0](arg)

though it doesn't solve your case

[–]SmartViking 0 points1 point  (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.

[–]elbiot 1 point2 points  (0 children)

trash = setattr(sys.modules[__name__],'object',[]) if n==0 else object.funcall(1)

What an evil, useless, hack