DEV Community

loading...

Discussion on: What is the most confusing thing to you in Python?

Collapse
math2001 profile image
Mathieu PATUREL

Let's not do that with PHP or JavaScript, otherwise it'll never end :smile:

For the string.join, it makes sense, although both choice (string.join or list.join) have inconvenient.

If you look at every single str methods (.title(), .lower(), .format(), .strip(), it returns a new string. It never mutate the original one.

For the list objects, it's the opposite. It always mutates the original list, and always returns None.

So, if they implemented the .join() method on the list objects, they would have had to break this "convention". If they didn't, this would have append:

>>> lines = ['first', 'second', 'third']
>>> lines.join(' ')
>>> lines
'first second third'

(I'm not even sure it's possible change the type of an existing object)

So, the choice they made doesn't break any conventions and is logic, although it might not be the prettiest. IMHO, it's right choice. And in the zen's opinion too, I guess:

Special cases aren't special enough to break the rules.

(guess they didn't respect this one for your last example though).

Good post anyway, learned something about python's int, thanks! :+1:

Collapse
r0f1 profile image
Florian Rohrer Author

Wow, thank you for your elaborate response! :)

Collapse
nicoxxl profile image
Nicolas B. • Edited

Python does not forbid many things so try this:

class Hello:
  def __init__(self, z):
    self.z = z

  def do(self):
    print('Hello {}'.format(self.z))


class SeeYou:
  def __init__(self, z):
    self.z = z

  def do(self):
    print('See you {}'.format(self.z))

y = Hello('world')
y.do()
y.__class__ = SeeYou
y.do()
Collapse
math2001 profile image
Mathieu PATUREL

:smiley: Never thought of this before... That's cool! Thanks!