0

Assume I have a simple array like this:

A = [1, 2, 3]

I can append or pop an element simply by doing:

A.append(0)
A.pop(0)

But if I turn this into a numpy array:

A = np.array(A)
A.append(0)

We get the following error (I know you can append an element to a numpy array but it requires more work):

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Because if we check their types we see that they belong to different classes (I want to say that they are different objects but I find this terminology confusing):

<class 'list'>
<class 'numpy.ndarray'>

So apparently I cannot use built-in functions (you would say methods) for an object that is created by another guy (for example you cannot use a list if you turn it into a NumPy array because it is not a list anymore)

Then my question is, is this really how it is supposed to be? I mean was this really intended when the object-oriented design was created or are these kinds of things fundamental design flaws?

26
  • 1
    "(I know you can append an element to a numpy array but it requires more work):" You cannot append an element to a numpy array, numpy arrays cannot change size. You can create a new, differently sized array though Commented Jun 2, 2022 at 17:11
  • 1
    "Then my question is, is this really how it is supposed to be? I mean was this really intended when the object-oriented design was created or are these kinds of things fundamental design flaws?" This is absolutly how it's supposed to be. This is not a design flaw, the whole point of a class is to define a type. Types define the behavior of a class. For example, I have an object, x = "33", that is a str object, of course, if I have another kind of object, y = 22, I can't use the same methods. This is a pretty fundamental concept of OOP Commented Jun 2, 2022 at 17:14
  • 1
    If you turn water into an ice cube, you can't pour it any more. Because liquid and solid are different things. Same with arrays and lists. Different things. Commented Jun 2, 2022 at 17:16
  • 1
    "(for example you cannot use a list if you turn it into a NumPy array because it is not a list anymore)" Well, you didn't turn it into a list. You created a completely different object, a numpy.ndarray object. The list still exists independently of the new object. This is btw, @Samwise why I don't like the "turn water into an ice cube" analogy, because you aren't mutating an object into a different type of object, you are creating a completely new object Commented Jun 2, 2022 at 17:21
  • 2
    There is a very important reason that numpy.ndarray objects don't define .append and .pop. This isn't a flaw, it is a carefully considered design choice. So no, it is exactly lie the int vs str case Commented Jun 2, 2022 at 17:25

1 Answer 1

3

The main point of having classes is to define abstractions - that is to define a thing and the operations it provides to its clients (that is, the interface it offers to its clients). Different classes serve different needs and therefore have different interfaces.

Sign up to request clarification or add additional context in comments.

1 Comment

Precisely! A "type" is an abstraction, and the way we create a "type" in class-based OOP languages is by defining a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.