Function Parameters Assigned with Object References

Q

Is it true that all function parameters are assigned with object references in Python?

✍: FYIcenter.com

A

Yes, all function parameters are assigned with object references in Python. You modify referenced data objects to share data with the calling statement.

Here is a good example of modifying the data object referenced by the parameter:

IndentationError: unexpected indent
>>> def load(name,age,user):
...     profile = {"name":name,"age":age}
...     user["profile"] = profile
...
>>> guest = {}
>>> load("Joe",25,guest)
>>> guest
{'profile': {'name': 'Joe', 'age': 25}}

Note that both variables "guest" and "user" are referring to the same data object.

 

Defining Functions in Python

⇒⇒Python Tutorials

2017-09-08, 121👍, 0💬