Kranis-97 Posted August 17, 2014 Share Posted August 17, 2014 def funktion(): return 2, 8 x = 0 y = 1 x,y += funktion() Ovanstående kod returnerar ett felmeddelande vid rad 5: "SyntaxError: illegal expression for augmented assignment" Det jag vill är att det värdena funktionen returnerar ska läggas till på x:s respektive y:s nuvarande värden. Alltså ska x=2 och y=9. Givetvis kan man låta två andra variabler agera "mellanhand" och hämta ut värdena från funktionen, men jag tycker att det borde finnas ett smidigare sätt, eller?? Link to comment Share on other sites More sharing options...
Phenomen Posted August 17, 2014 Share Posted August 17, 2014 import operator def foo(): return (2, 8) x = 2 y = 4 sum_tuple = map(operator.add, (x,y), foo() ) print sum_tuple Link to comment Share on other sites More sharing options...
Kranis-97 Posted August 18, 2014 Author Share Posted August 18, 2014 Tack, i Python 2.7.6 fungerar det utmärkt, men i 3.4.1 får jag ut följande: <map object at 0x000000000224A3C8> Någon idé? Link to comment Share on other sites More sharing options...
Phenomen Posted August 18, 2014 Share Posted August 18, 2014 import operator def foo(): return (2, 8) x = 2 y = 4 sum_tuple = tuple(map(operator.add, (x,y), foo())) print(sum_tuple) Link to comment Share on other sites More sharing options...
Phenomen Posted August 18, 2014 Share Posted August 18, 2014 map returnerar iterator-object istället för array i v3 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.