News for hackers. Stuff that matters.
Re:The PEPs of Python 3.9 by Erlangen on Thursday May 28, 2020 @03:23PM (#23341328)
Slicing in Python always create a new object. You can test it with a list of integers..
Re:The PEPs of Python 3.9 by cecilpl2 on Thursday May 28, 2020 @04:57PM (#23342507)
My favorite example of something similar to this, since you brought it up:

  >>> a = [254, 255, 256, 257, 258]
  >>> b = [254, 255, 256, 257, 258]
  >>> for i in range(5): print(a[i] is b[i])
  ...
  True
  True
  True
  False
  False
In Python, integers in the range [-5, 256] are statically constructed in the interpreter and refer to fixed instances of objects. All other integers are created dynamically and refer to a new object each time they are created.
Re:The PEPs of Python 3.9 by mark-r on Thursday May 28, 2020 @07:55PM (#23344593)
Leaky abstractions at its finest.
Re:The PEPs of Python 3.9 by sullyj3 on Thursday May 28, 2020 @10:12PM (#23345594)
I mean, people should be using `==` for this. The fact that `is` happens to work for small numbers is an implementation detail that shouldn't be relied upon.
Re:The PEPs of Python 3.9 by mark-r on Thursday May 28, 2020 @11:10PM (#23345896)
Absolutely. But because it does work they might start using it without knowing it's wrong, then be surprised when it doesn't work. Python has other areas where the common English definition of a word leads to misunderstandings about how they're to be used.
Re:The PEPs of Python 3.9 by kgm on Thursday May 28, 2020 @10:26PM (#23345689)
Though "when" the object is created isn't always so straightforward:

  >>> x = 257
  >>> y = 257
  >>> x is y
  False
  >>> def f():
  ...     x = 257
  ...     y = 257
  ...     return x is y
  ...
  >>> f()
  True
The lesson being that `is` is essentially meaningless for immutable objects, and to always use `==`.
Re:The PEPs of Python 3.9 by pansa2 on Thursday May 28, 2020 @10:39PM (#23345759)
> `is` is essentially meaningless for immutable objects

OTOH it’s recommended to use `is` rather than `==` when comparing to “singletons like `None`”.

https://www.python.org/dev/peps/pep-0008/#programming-recomm...

Re:The PEPs of Python 3.9 by joppy on Friday May 29, 2020 @01:52AM (#23346659)
What is the rationale behind this? '==' works all the time, and 'is' only works sometimes. Using 'is' wherever possible requires the user to know some rather arbitrary language details (which objects are singletons and which are not), wheras '==' will always give the correct answer regardless.
Re:The PEPs of Python 3.9 by pansa2 on Friday May 29, 2020 @02:41AM (#23346917)
Classes can overload `==`:

    class C:
        def __eq__(self, other):
            return True

    print(C() == None)  # True
    print(C() is None)  # False
Re:The PEPs of Python 3.9 by masklinn on Thursday May 28, 2020 @03:44PM (#23341656)
> Slicing in Python always create a new object.

It always creates a new object but it doesn't necessarily copy the contents (even shallowly).

For instance slicing a `memoryview` creates a subview which shares storage with its parent.

Re:The PEPs of Python 3.9 by ianhorn on Thursday May 28, 2020 @03:34PM (#23341527)
It'll always create a new object but my understanding is that at least in numpy the new and old object will share memory. Am I wrong there too?
Re:The PEPs of Python 3.9 by aidos on Thursday May 28, 2020 @03:47PM (#23341693)
Correct. In Numpy the slices are views on the underlying memory. That’s why they’re so fast, there’s no copying involved. Incidentally that’s also why freeing up the original variable doesn’t release the memory (the slices are still using it).
Re:The PEPs of Python 3.9 by dialamac on Thursday May 28, 2020 @03:44PM (#23341664)
CPython is pretty terrible. Numpy has the concept of views, cpython doesn’t do anything sophisticated.
Re:The PEPs of Python 3.9 by throwaway287391 on Thursday May 28, 2020 @09:21PM (#23345265)
> Slicing in Python always create a new object.

Since you said "always", I'll be "that guy"...

For the builtin types, yeah. But not in general; user code can do whatever it wants:

    class MyType:
      def __getitem__(self, *args):
        return self
    my_obj = MyType()
    my_other_obj = my_obj[3:7]
    assert my_other_obj is my_obj