944,098 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5699
  • Python RSS
Jan 18th, 2006
0

"object , object" concatination?

Expand Post »
hey yo's,

i pretty much understand this function, except for the return value. i know that string , string concats the strings. but what does the comma in the middle do when you have 2 different objects on either side as in the return value below?

Python Syntax (Toggle Plain Text)
  1. def load_image(name, colorkey=None):
  2. fullname = os.path.join('data', name)
  3. try:
  4. image = pygame.image.load(fullname)
  5. except pygame.error, message:
  6. print Cannot load image:, name
  7. raise SystemExit, message
  8. image = image.convert()
  9. if colorkey is not None:
  10. if colorkey is -1:
  11. colorkey = image.get_at((0,0))
  12. image.set_colorkey(colorkey, RLEACCEL)
  13. return image, image.get_rect()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
senateboy is offline Offline
5 posts
since Jan 2006
Jan 18th, 2006
0

Re: "object , object" concatination?

Hi!

Quote originally posted by senateboy ...
i know that string , string concats the strings
Huh? Can you give an example?
Quote originally posted by senateboy ...
what does the comma in the middle do when you have 2 different objects on either side as in the return value below?
It just separates them So you can return more than one value. Have a look:
Python Syntax (Toggle Plain Text)
  1. def lala(a,b):
  2. a += 2
  3. b += 2
  4. # return both, the new a and the new b
  5. return a, b
  6.  
  7. x, y = lala(1,2)
  8. print x, y

Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Jan 18th, 2006
0

Re: "object , object" concatination?

commas act as concatination in this example:
Python Syntax (Toggle Plain Text)
  1. #string concatination with a comma
  2. "i love cities" , "like San Francisco"
this produces the string "i love cities like San Francisco"

ok so i understand now that a function can return 2 objects. so what is the function techincaly returning? a list? list of objects. once more? how does the "," comma act in your example:
Python Syntax (Toggle Plain Text)
  1. x, y = lala(1,2)

i just dont see how it werks. i can memorize it but id rather understand it under the hood.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
senateboy is offline Offline
5 posts
since Jan 2006
Jan 18th, 2006
0

Re: "object , object" concatination?

Maybe this example from the "Starting Python" sticky will help ...
[php]
# use a tuple to return multiple items from a function
# a tuple is a set of values separated by commas
def multiReturn():
return 3.14, "frivolous lawsuits", "Good 'N' Plenty"

# show the returned tuple
# notice that it is enclosed in ()
print multiReturn()

# load to a tuple of variables
num, str1, str2 = multiReturn()
print num
print str1
print str2

# or pick just the element at index 1, should be same as str1
# tuples start at index zero just like lists etc.
print multiReturn()[1]
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 18th, 2006
0

Re: "object , object" concatination?

Quote originally posted by senateboy ...
commas act as concatination in this example:
No it does not concatenate! Or am I silly?
Python Syntax (Toggle Plain Text)
  1. x = "a", "b"
  2. print x # -> ('a', 'b')
It creates a tuple, and that's what is returned in my example, a tuple.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Jan 19th, 2006
0

Re: "object , object" concatination?

I think the following confuses our friend senateboy
Python Syntax (Toggle Plain Text)
  1. print "hot", "dog" # -> hot dog
  2.  
  3. str1 = "hot", "dog"
  4. print str1 # -> ('hot', 'dog')
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Best way to convert C++ coding to Python
Next Thread in Python Forum Timeline: python IDE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC