+ 31
Why copy is a function and view is a method in python?
Syntax of copy: ndarray.copy() Syntax of view: ndarray.view()
33 Antworten
+ 5
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/JUMP_LINK__&&__Python__&&__JUMP_LINK/2470/
+ 16
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.
+ 14
Thank you for helping me Alexandr
+ 14
Thank you too Tibor Santa
+ 14
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....
+ 14
Tibor Santa good example .I got clarity on that.:-)
+ 14
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.👍
+ 14
Thank you so much :-) for answering my qn correct answer and answering my doubts with patience
+ 13
But it is not written inside class
+ 13
HonFu
Here it is there:
https://www.tutorialspoint.com/numpy/numpy_copies_and_views.htm
+ 13
Matthew Shirvan thank you for your explanation 👍
+ 13
OGUAJU PATRICK TQ for ur explanation 👍
+ 13
HonFu Good example I too thought abt same thing as their syntax will comes with dot followed by them😅 tq fr ur explanation 👍
+ 12
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 .
+ 12
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
+ 12
Kishalaya Saha did u mean it?
+ 11
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))
+ 8
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.
+ 8
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.
+ 7
Both are listed as methods in the official docs.
Copy:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.copy.html
View:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.view.html