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()

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

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.

Maybe this example from the "Starting Python" sticky will help ...

# 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]

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.

I think the following confuses our friend senateboy

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

str1 = "hot", "dog"
print str1          # -> ('hot', 'dog')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.