"object , object" concatination?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2006
Posts: 5
Reputation: senateboy is an unknown quantity at this point 
Solved Threads: 0
senateboy senateboy is offline Offline
Newbie Poster

"object , object" concatination?

 
0
  #1
Jan 18th, 2006
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?

  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()
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: "object , object" concatination?

 
0
  #2
Jan 18th, 2006
Hi!

Originally Posted by senateboy
i know that string , string concats the strings
Huh? Can you give an example?
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 5
Reputation: senateboy is an unknown quantity at this point 
Solved Threads: 0
senateboy senateboy is offline Offline
Newbie Poster

Re: "object , object" concatination?

 
0
  #3
Jan 18th, 2006
commas act as concatination in this example:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: "object , object" concatination?

 
0
  #4
Jan 18th, 2006
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: "object , object" concatination?

 
0
  #5
Jan 18th, 2006
Originally Posted by senateboy
commas act as concatination in this example:
No it does not concatenate! Or am I silly?
  1. x = "a", "b"
  2. print x # -> ('a', 'b')
It creates a tuple, and that's what is returned in my example, a tuple.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: "object , object" concatination?

 
0
  #6
Jan 19th, 2006
I think the following confuses our friend senateboy
  1. print "hot", "dog" # -> hot dog
  2.  
  3. str1 = "hot", "dog"
  4. print str1 # -> ('hot', 'dog')
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 5228 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC