954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

"object , object" concatination?

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?

def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print Cannot load image:, name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()
senateboy
Newbie Poster
5 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

Hi!

i know that string , string concats the strings


Huh? Can you give an example?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:

def lala(a,b):
    a += 2
    b += 2
    # return both, the new a and the new b
    return a, b

x, y = lala(1,2)
print x, y


Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

commas act as concatination in this example:

#string concatination with  a comma 
"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:

x, y = lala(1,2)


i just dont see how it werks. i can memorize it but id rather understand it under the hood.

senateboy
Newbie Poster
5 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

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]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
commas act as concatination in this example:


No it does not concatenate! Or am I silly? :)

x = "a", "b"
print x  # -> ('a', 'b')

It creates a tuple, and that's what is returned in my example, a tuple.

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

I think the following confuses our friend senateboy

print "hot", "dog"  # ->  hot dog

str1 = "hot", "dog"
print str1          # -> ('hot', 'dog')
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You