0

I know that python does not support pointers! But recently I've noticed something about OpenCV-python! The following line will draw a circle on a given image with the specified center and radius,

cv2.circle(image, (x, y), r, (0, 0, 00), thickness=3)

according to the cv2.circle doc, the return value is None. So all the changes happen on the given image. But we know that python doesn't support pointers! So if it is possible to do so, how!? Does any one can help or provide a link!

Hadi
  • 5,328
  • 11
  • 46
  • 67
  • are you worried about the img getting manipulated ? – berak Apr 09 '14 at 17:27
  • No, I want to know if it is possible to have pointers how we can use this tool in other applications! – Hadi Apr 09 '14 at 17:28
  • You wud definitely want to read this [ Semantics of Python variable names from a C++ perspective](http://rg03.wordpress.com/2007/04/21/semantics-of-python-variable-names-from-a-c-perspective/) – Abhinav Apr 09 '14 at 17:34

3 Answers3

4

Well, python does not have pointers, but the objects are passed to functions by reference (and so in java, another language without pointers). This mechanism is very similar to passing pointers by value in C.

The variable image is merely a reference to an object, NOT the object itself. When you pass image to the function cv2.circle, you actually pass the reference by value. It is the local copy of the reference in the function, but they essentially point to the same object in memory. That's how image gets modified in cv2.circle.

Suggested reading: What's the difference between passing by reference vs. passing by value?

and How do I pass a variable by reference? (thanks, @Wooble!)

Community
  • 1
  • 1
Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • Python does not use pass by reference. At least not in the way that term is usually understood. ( http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference has a lot of good answers) – Wooble Apr 09 '14 at 18:29
  • @Wooble pass by reference is certainly a case of pass by value :) and that's a great resource! – Sufian Latif Apr 09 '14 at 18:30
  • @0606002 Passing a reference by value is different from the "Pass by reference". Pass by reference would require reassignments of parameters in the called function to cause reassignments in the caller as well. This is the semantic that C++ uses for pass by reference and is not what python does. – Ioan Alexandru Cucu Apr 10 '14 at 09:50
1

I suggest you read the evaluation strategy article on wikipedia.

Python does call by sharing (which is a synonym of saying: passes a reference to the object by value)

I don't think I would be able to explain it better than the way those articles do.

Ioan Alexandru Cucu
  • 11,981
  • 6
  • 37
  • 39
0

Python doesn't give you access to pointers when you code. You can't declare a pointer, but of course it used pointers internally.

The language doesn't have pointers, but the implementation uses pointers.

Luke
  • 708
  • 5
  • 13