Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does numpy do this?
#1
Hello,
let me explain what I mean with this example

array = np.array([[2, 0], [5, 9]])  
print(array)           # Result: [[2, 0], [5, 9]]
print(type(array))     # Result: <type 'numpy.ndarray'>
print(array.flatten()) # Result: [2 0 5 9]
So as you can see arrat is a type 'numpy.ndarray' but us returned as a list of lists. I believe that __repr__ and __str__ only allow to change the object representation as a string. Here we get a list and what is really nice is that you can use methods like .flatten() modify it.

I looked into the __new__ help but I didn't get much out of it.
__new__(...)
T.__new__(S, ...) -> a new object with type S, a subtype of T

Can any someone ilustrate with an example a similar behaviour. I've trying without success to mimic that playing with __new__,
but I must be doing something wrong.
Reply


Messages In This Thread
How does numpy do this? - by rudihammad - Jun-19-2020, 06:08 AM
RE: How does numpy do this? - by ndc85430 - Jun-19-2020, 06:14 AM
RE: How does numpy do this? - by rudihammad - Jun-19-2020, 06:41 AM
RE: How does numpy do this? - by buran - Jun-19-2020, 06:43 AM
RE: How does numpy do this? - by buran - Jun-19-2020, 06:44 AM
RE: How does numpy do this? - by rudihammad - Jun-19-2020, 07:17 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020