Live data from Hacker News

Python Oddities

twitter.com

31–38 of 38 posts

Re: Python Oddities

#31

    >>> a = [1]
    >>> a += (2,)
    >>> a
    [1, 2]

    >>> b = (1,)
    >>> b += [2]
    TypeError
It's an oddity that an immutable type cannot be mutated in place?

Re: Python Oddities

#32
post #31

>>> a = [1] >>> a += (2,) >>> a [1, 2] >>> b = (1,) >>> b += [2] TypeError It's an oddity that an immutable type cannot be mutated in place?

This one is an odd one because initially most people I show this one to assume the oddity is that immutable types cannot be mutated in-place.

But tuples can be mutated in-place: >>> b = (1,) >>> b += (2,) >>> b (1, 2)

The odd thing is that lists can be in-place added (+='d) to any iterable, while tuples can only be in-place added to tuples.

I think it works this way so that += on lists works consistently with the extend method on lists, which also accepts any iterable.

Re: Python Oddities

#33
post #7
post #5

Best use of Twitter Moments I've ever seen.

First use of Twitter Moments I've ever seen. What is this? A collection of tweets? I'm confused because they all seem to be by the author, did he tweet them himself and collected them? The page's UI looks weird, I'm not sure if Twitter had a redesign or if I'm blocking some of their CSS.

I think they're pretty much a collection of tweets. This was the first time I used moments and I'm not sure where I saw them used before I made this.

They're mostly by me because I made a goal for myself to tweet out one of these every week for a year. Others have shared their own and I've tweeted my own since then, but Twitter's hashtag search is pretty bad so I made a moment.

Re: Python Oddities

#34
post #10

Good thing CPython is the spec for the language. That said, many of these "oddities" are disingenuous to the point of lying, many of the error messages are omitted. Or the functionality converts to list before doing an operation. Not that it is good library or language design, but this is a great way to inflate bug counts.

I think you may be taking the word oddity different than I meant it. These are things that trip up my students when they're learning Python.

I left out error messages (and occasionally other things) because Twitter.

I don't think any of the ones I put in this moment should be considered bugs. Many of these are core language features.

Re: Python Oddities

#35
post #34
post #10

Good thing CPython is the spec for the language. That said, many of these "oddities" are disingenuous to the point of lying, many of the error messages are omitted. Or the functionality converts to list before doing an operation. Not that it is good library or language design, but this is a great way to inflate bug counts.

I think you may be taking the word oddity different than I meant it. These are things that trip up my students when they're learning Python. I left out error messages (and occasionally other things) because Twitter. I don't think any of the ones I put in this moment should be considered bugs. Many of these are core language features.

Some of them seem like litmus test for a Python programmer interview, others feel like WTFs Python LoL! It is hard to disambiguate them. Standing on their own in that twitter feed, with little context, there is next to no enlightenment.

Lots of the oddities could be explained by

    In [1]: list({'a':1, 'b':2, 'c':3})
    Out[1]: ['a', 'b', 'c']
When people would expect

    In [2]: list({'a':1, 'b':2, 'c':3}.items())
    Out[2]: [('a', 1), ('b', 2), ('c', 3)]
Lots of Python makes sense. About 3% does not. It is good to know the 3% so it doesn't bite one in the ass. Maybe switch to image posts? 1 megapixel limit.

Re: Python Oddities

#36
post #33
post #7

Earlier quoted context omitted.

First use of Twitter Moments I've ever seen. What is this? A collection of tweets? I'm confused because they all seem to be by the author, did he tweet them himself and collected them? The page's UI looks weird, I'm not sure if Twitter had a redesign or if I'm blocking some of their CSS.

I think they're pretty much a collection of tweets. This was the first time I used moments and I'm not sure where I saw them used before I made this. They're mostly by me because I made a goal for myself to tweet out one of these every week for a year. Others have shared their own and I've tweeted my own since then, but Twitter's hashtag search is pretty bad so I made a moment.

I think it works quite well, I just hadn't seen the feature before. I disagree with you a bit, though, in that many seemed pretty regular, and weren't really oddities, in my opinion.

Re: Python Oddities

#37
post #32
post #31

>>> a = [1] >>> a += (2,) >>> a [1, 2] >>> b = (1,) >>> b += [2] TypeError It's an oddity that an immutable type cannot be mutated in place?

This one is an odd one because initially most people I show this one to assume the oddity is that immutable types cannot be mutated in-place. But tuples can be mutated in-place: >>> b = (1,) >>> b += (2,) >>> b (1, 2) The odd thing is that lists can be in-place added (+='d) to any iterable, while tuples can only be in-place added to tuples. I think it works this way so that += on lists works consistently with the ext…

> But tuples can be mutated in-place: >>> b = (1,) >>> b += (2,) >>> b (1, 2)

That's not an in-place mutation any more than

    >>> a = 1
    >>> a += 2
is.

The oddity is that most people assume `a += b` is the same as `a = a + b`, but Python allows overloading both separately and list.__iadd__ is an alias for list.extend, hence being able to += any iterable onto a list whereas `list + non_list` generates a type error.

The original intent is an optimisation (to avoid copying the subject list) but it's a broken one as it behaves very inconsistently with regular concatenation/addition: it mutates the subject in-place and allows a different (wider) set of parameters.

Re: Python Oddities

#38

Why does the _MyClass__number example work the way it does?

Within class bodies, symbols prefixed with two underscores get "mangled" (prefixed with the class name).

This is intended to avoid risks of collisions for classes built specifically for inheritance so you'd usually use it with attributes e.g. `self.__name = name` (that way if a subclass defines its own "name" things will work as expected for both). I was unaware this also worked for straight local variables.

I was unaware it happened for all symbols, but I guess it makes sense as the Python compiler does very little analysis.

Post reply on HN