Why copy is a function and view is a method in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 31

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

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

24th Jan 2020, 5:14 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
31 Answers
+ 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/
26th Jan 2020, 4:36 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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.
24th Jan 2020, 6:55 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 14
Thank you for helping me Alexandr
24th Jan 2020, 6:17 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 14
Thank you too Tibor Santa
24th Jan 2020, 6:17 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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....
26th Jan 2020, 1:33 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 14
Tibor Santa good example .I got clarity on that.:-)
26th Jan 2020, 1:37 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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.👍
26th Jan 2020, 5:27 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 14
Thank you so much :-) for answering my qn correct answer and answering my doubts with patience
26th Jan 2020, 5:54 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 13
But it is not written inside class
24th Jan 2020, 5:43 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
24th Jan 2020, 6:33 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 13
Matthew Shirvan thank you for your explanation 👍
25th Jan 2020, 5:23 PM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 13
OGUAJU PATRICK TQ for ur explanation 👍
26th Jan 2020, 1:39 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 13
HonFu Good example I too thought abt same thing as their syntax will comes with dot followed by them😅 tq fr ur explanation 👍
26th Jan 2020, 1:43 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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 .
26th Jan 2020, 3:38 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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
26th Jan 2020, 3:56 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 12
Kishalaya Saha did u mean it?
26th Jan 2020, 5:28 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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))
25th Jan 2020, 8:33 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 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.
24th Jan 2020, 6:08 PM
Tibor Santa
Tibor Santa - avatar
+ 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.
24th Jan 2020, 6:39 PM
HonFu
HonFu - avatar