+20

Why copy is a function and view is a method in python?

Syntax of copy: ndarray.copy() Syntax of view: ndarray.view()

1/24/2020 5:14:49 PM

रूबा

33 Answers

New Answer

+4

Oh, so are you talking about ndarray.__copy__ vs ndarray.copy? They are both methods of the ndarray class. The first one is used when someone calls the copy function of the copy module on a numpy array, like this: import numpy as np import copy a = np.array([1, 2]) b = copy.copy(a) The result is the same as doing b = a.copy() Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.__copy__.html#numpy.ndarray.__copy__ Some people call this operator overloading, which you can read about here: https://www.sololearn.com/learn/Python/2470/

+11

Thank you HonFu at least u have tried to answer my qn on numpy😊 .I too had exactly same knowledge abt methods as u 😅bt I got some more info abt them after posting this qn.

+10

Thank you for helping me Alexandr

+10

Thank you too Tibor Santa

+10

Walter I too listened abt it but I didn't knew abt it perfectly.So I mentioned in my qn abt their syntaxes due to my too little knowledge on ur answr.:-) Bt they have same type of syntax....

+10

Tibor Santa good example .I got clarity on that.:-)

+10

Wow good information .So in ur view there is copy method and copy function (which is in copy module) i.e magicmethods .I understood ur point.👍

+10

Thank you so much :-) for answering my qn correct answer and answering my doubts with patience

+9

But it is not written inside class

+9

HonFu Here it is there: https://www.tutorialspoint.com/numpy/numpy_copies_and_views.htm

+9

Matthew Shirvan thank you for your explanation 👍

+9

OGUAJU PATRICK TQ for ur explanation 👍

+9

HonFu Good example I too thought abt same thing as their syntax will comes with dot followed by them😅 tq fr ur explanation 👍

+9

Kishalaya Saha did u mean it?

+8

import numpy as np a = np.array([10,10]) print(type(a)) print(dir(a))

+8

Kishalaya Saha yeah there is copy() function of copy module we r nt talking abt that it's totally different than this copy syntactically and functionally .

+7

Kishalaya Saha yeah ofcourse,it acts as a method and as a function sometimes ... U will get idea of that in this program import numpy as np a = np.array([10,10]) print(type(a)) print(dir(a))

+7

Kishalaya Saha In that program output it has shown all methods and functions of numpy array observe in that functions it has shown copy and in methods it has shown copy and view so by this we can understand it acts as a function and method sometimes

+6

Technically both of them are methods because they are associated with the ndarray object. But a method is also a function in general, so you can use the two words synonymously in this case.

+6

Unfortunately I haven't used NumPy myself and don't know what's a function and what's a method. Usually, as has been said before, a method is tied to a class while a function is free from a class. But it does get mixed up here and there, and at the edges (when you get to static methods) the lines seem to get blurry anyway. The more important thing for you - and that's not related to class or not class - is the differentiation between a 'copy' and a 'view'. This problem field - sometimes with different names - comes up a lot in Python, and that's something you need to have a firm grasp of.