0
def inheriting():
    results = set()

    resultsAdd(results)

    print(results)
    return None

def resultsAdd(results):
    results.add("3")

    return None

inheriting()

here for example, i expected inheriting() it to print an empty set. if i change it to

results = "someString"

and did results = "otherString" to that in resultsAdd(results)

it just prints "someString".

I want to learn more about this but I don't know what i would look for. Can someone explain or tell me where I can learn more about these?

han Park
  • 25
  • 5

1 Answers1

0

If you have not read the tuturial that comes with python, you should do so. But here is a summary explanation of this common newbie question.

First part: An assignment statement like a = <expression1> connects name a with the object resulting from evaluating <expression1>. Given def f(a): pass, f(<expression1>) connects parameter name a with the object resulting from evaluating <expression1>. In both cases, object1 is not copied.

Second: if object a is a set, def f(a): a.add(<expression2>) mutates (changes) object a by adding object2, resulting from evaluating <expression2>, to the set. On the other hand, def f(a): a = <expression2>, ignores the original object1 and rebinds local name a to object2.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52