WonkasWilly
WonkasWilly

Reputation: 47

How are these two functions the same?

Here's the code that Zed Shaw provides in Learning Python the Hard Way:

ten_things = "Apples Oranges Crows Telephone Light Sugar"

print "Wait there's not 10 things in that list, let's fix that."

stuff = ten_things.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]

while len(stuff) != 10:
    next_one = more_stuff.pop()
    print "Adding: ", next_one
    stuff.append(next_one)
    print "There's %d items now." % len(stuff)

print "There we go: ", stuff

print "Let's do some things with stuff."

print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool!
print '#'.join(stuff[3:5]) # super stellar!

Then on one of the study drills, he says:

  1. Translate these two ways to view the function calls. For example, ' '.join(things) reads as, “Join things with ‘ ‘ between them.” Meanwhile, join(' ', things) means, “Call join with ‘ ‘ and things.” Understand how they are really the same thing.

My problem is, I'm having a tough time seeing how they're the same thing? To my understanding, the first function is saying take whatever is in things, and concatenate them with ' '. But the second function (to my knowledge), is saying call join, while using ' ' and things as an argument? Sort of the way you would use them when defining a function? I'm pretty lost on this...could you guys could clarify on this?

Upvotes: 3

Views: 232

Answers (1)

El'endia Starman
El'endia Starman

Reputation: 2244

To be precise, ''.join(things) and join('',things) are not necessarily the same. However, ''.join(things) and str.join('',things) are the same. The explanation requires some knowledge of how classes work in Python. I'll be glossing over or ignoring a lot of details that are not totally relevant to this discussion.

One might implement some of the built-in string class this way (disclaimer: this is almost certainly not how it's actually done).

class str:
    def __init__(self, characters):
        self.chars = characters

    def join(self, iterable):
        newString = str()

        for item in iterable:
            newString += item #item is assumed to be a string, and += is defined elsewhere
            newString += self.chars

        newString = newString[-len(self.chars):] #remove the last instance of self.chars

        return newString

Okay, notice that the first argument to each function is self. This is just by convention, it could be potatoes for all Python cares, but the first argument is always the object itself. Python does this so that you can do ''.join(things) and have it just work. '' is the string that self will be inside the function, and things is the iterable.

''.join(things) is not the only way to call this function. You can also call it using str.join('', things) because it's a method of the class str. As before, self will be '' and iterable will be things.

This is why these two different ways to do the same thing are equivalent: ''.join(things) is syntactic sugar for str.join('', things).

Upvotes: 7

Related Questions