0

I know that functions in python always return references of variables. I was curious if the getter methods of properties return deep copies, or if the getter methods are only formalities and return references as well.

  • 2
    "I know that functions in python always return shallow copies of variables" No, they do not. Variables aren't copied anyway, *objects* are copied. Python never implicitly copies objects. The only times copies are created are when you tell them to be created. `property.__get__` works like any other function in that regard – juanpa.arrivillaga Dec 14 '18 at 23:05
  • https://stackoverflow.com/questions/17330160 – Robert Harvey Dec 14 '18 at 23:06
  • @RobertHarvey sure, but I think OP has an even more fundamental misunderstanding here. – juanpa.arrivillaga Dec 14 '18 at 23:07
  • @juanpa.arrivillaga: I'm not sure you're explaining it correctly. If Python works like most other programming languages, a copy of *something* is always made during parameter passing: either a *reference* or a *value* – Robert Harvey Dec 14 '18 at 23:08
  • @RobertHarvey a `PyObject` pointer is passed by value underneath the hood in CPython. That is neither here nor there though. Anyway, there are many different [evaluation strategies](https://en.wikipedia.org/wiki/Evaluation_strategy). In principle, Python could be implemented without using call by value in C underneath the hood. – juanpa.arrivillaga Dec 14 '18 at 23:11

1 Answers1

5

All methods return references to objects unless you explicitly tell them to make copies (see here). A getter method works the same as any other method, meaning that it will return a reference rather than a copy. However, if you want to return a copy, you can use the copy module:

import copy

myvar = "abc"
myvar2 = copy.copy(myvar)

In this code, changing myvar will not change myvar2 and vice versa. However, if you do this:

myvar = "abc"
myvar2 = myvar

modifying myvar will also modify myvar2 and vice versa, because myvar and myvar2 refer to the same object.

Python uses something called call by sharing when passing arguments to functions. This basically means that a reference to an object is passed to functions. This allows modifying of the arguments passed to a function.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • A copy is still a reference. – juanpa.arrivillaga Dec 14 '18 at 23:11
  • @juanpa.arrivillaga technically, yes, but it is not a reference to the variable it was copied from. – Pika Supports Ukraine Dec 14 '18 at 23:12
  • 3
    References aren't to *variables*, they are to *objects*. I think you need to be precise, because OP is quite confused. – juanpa.arrivillaga Dec 14 '18 at 23:13
  • The elephant in the room: the OP needs a better understanding of *pass by value* and *pass by reference* (and passing references by value). – Robert Harvey Dec 14 '18 at 23:16
  • 2
    @RobertHarvey the term you are looking for is [*call by sharing*](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing), sometimes termed "call by assignment". The whole "passing references by value" is a Javaism. Python is a pure OOP language, everything is an object. – juanpa.arrivillaga Dec 14 '18 at 23:18
  • @juanpa.arrivillaga: Hmm, the things I'm reading on the Internet suggest that, while things happen differently under the hood, the net outcome is the same: Python appears to essentially have "call by value" and "call by reference" semantics, internal implementation details notwithstanding. – Robert Harvey Dec 14 '18 at 23:29
  • Ah, I think I get it. Primitives in Python are always *boxed.* – Robert Harvey Dec 14 '18 at 23:30
  • 1
    @RobertHarvey well really, **there are no primitive types in Python**. Everything is full-fledged object. That's why you can do `(42).to_bytes(8, 'big')`. Anyway, Python definitely does not even appear to have call by reference. I can understand thinking it has call by value – juanpa.arrivillaga Dec 14 '18 at 23:39