2

I have some previous experience with C++ and just getting started up with Python. I read this text from Dive into Python :

enter image description here

In my experience, a general idea is, if you want to perform an operation on object 'O', you call a method on that object to do it.

Eg. If I have a list object, and I want to get summation of all elements, I would do something like :

listObject.getSumOfAllElements()

However, the call given in above book excerpt looks a little odd to me. To me this would make more sense :

return (["%s=%s" % (k, v) for k, v in params.items()]).join(";")

i.e. join all the elements of the list as a string, and here, use this parameter ';' as a delimiter.

Is this a design difference or just syntactically a little different than what I am thinking ?


Edit:

For completion, the book says this a little later :

enter image description here

Amit Tomar
  • 4,800
  • 6
  • 50
  • 83

1 Answers1

5

Is this a design difference or just syntactically a little different than what I am thinking?

Yes, I think it is by design. The join function is intentionally generic, so that it can be used with any iterable (including iterator objects and generators). This avoids implementing join for list, tuple, set etc separately.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497