Np.array(): function or method?

Hello,

I am trying to conceptually categorize the numpy.array() constructor function for my understanding. In one of the missions, datetime.datetime.strptime() was introduced as a constructor method (like the init method) defined within the datetime class, which is inside the datetime module. Keeping this in mind, how would one classify numpy.array()?

Here’s my stab at it: numpy.array() is NOT a method defined within any class. It is a constructor function within the numpy module that takes in certain arguments and outputs an ndarray object.

Please let me know if this classification of numpy.array() is correct. Going through the Python OOP mission, my understanding was that constructors are always methods defined within classes. But looking at numpy.array(), this might not always be the case.

Thank you,
Maddy

1 Like

Hi Maddy!

You are absolutely right - numpy.array() is a function and not a method.

I guess what makes it confusing is the . notation, as this is also how methods are called on the objects of a class.

Here, the . is used to specify that you want to use the array() function that is defined inside the numpy module. In other words, you are telling Python which namespace to search in for the function you need. Does this make sense?

1 Like

Thank you Slavina for your answer. Helped clear my doubt !

You’re most welcome!

If you consider your question to be solved, you can mark it as such for the benefit of other students like so:

To mark an answer correct:

  1. Scroll down to the correct post.
  2. Click on the three dots next to the reply button.
  3. Click the check box. It will then show up in the original post as “solved”.
1 Like