variables/objects passed by reference/value

Posted on 2014-07-23
Last Modified: 2014-07-29

This is because in python (unlike php) all variables are passed by reference, so they are generally considered to be mutable, however because some variable types in python are more complex, then they are immutable

I have read Ray's variables vs references article many times but I can not relate to python
Please give a php and python example of how variables are passed by reference
And if immutable in python, is variable passed by value? Relate this to php please.
Question by:rgb192
  • 3
  • 2
  • 2
LVL 28

Accepted Solution

pepr earned 250 total points
ID: 40216119
Have a look at Python basics -- illustrated (part 3).

Whenever a Python variable appears in the source, you can be sure that there is some hidden dictionary where the name of the variable (as a string) is used as the key to the dictionary, and that the reference value (basically the address) is used as the value in the dictionary.

Whenever you access a Python object, you do it indirectly, via a reference to the object. Whenever you assign a variable, you only store a copy of the reference to the target object.

Whenever you pass any object in Python (and it does not matter whether it is mutable or immutable), you pass the reference value (the address of the target object when simplified a bit).

Actually, Python objects do not move during their existence. They sit on the same memory address and the address is used as their identity number (simplified). Only the references are copied (passed) on various places. Whenever you get the reference, you are able to reach the target object. The target object counts references that point to it; hence, it does not destroy itself if any reference exists.

For PHP vs. Python and mutability... In many languages the string type is used as an array of characters and the characters can be modified. This way, the same string object can change its content during lifetime. In other languages (like Python or Java), once the string is constructed, it cannot change its content. It is a kind of artificial restriction. Accepting that, you can pass the object by reference value, and you know that all parties will see the same string. If the string were mutable, you would have to copy the string if you wanted to pass the exact content.

References and mutability terms are orthogonal (unrelated, not affecting each other). You simply cannot modify a string in Python. Whenever you think it looks like you are doing so, the operation actually creates a modified copy of the original.
LVL 33

Expert Comment

ID: 40217271
rgb192, , you may get experts to give you "Explanations" about programming things in python that may not relate to PHP at all.

If I may ask, what are you now trying to do in "python" programming (to lean it), that you have to ask this question, in other words, if you can be more specific in questions about learning python, then you may can get more helpfull answers.

Author Comment

ID: 40217350
If I may ask, what are you now trying to do in "python" programming (to lean it)

python basics illustrated part 3

I found part 2 but permission is denied

and I can not find part 1

I will read part 3 but may not understand until I read part1 and part2 first.

I like the visual diagram of examples of references and variables.  Ray wrote an article in honor of me for php (but I can not find). And Ray's article sometimes would be blocked 'permission denied'.
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

LVL 33

Assisted Solution

Slick812 earned 250 total points
ID: 40217487
OK, i read your comment, and I briefly looked at pages for a beginner to learn, , that seems too academic for many to use,  you may can do well, if you start at the "Source for Python" at, and look at their Beginners Guide at, and there is a Difference in using programming in python ver 2 and using python ver 3 .

you may look at their list for tutorial links -,  but I am in no position to recommend any tutorial to you for Python programming. You may do well to ask a question in the EE Python zone, and ask experts to recommend a Python tutorial to use

Again, I will say that trying to "relate" python programming to php programming may be more confusing than helpful.
LVL 28

Expert Comment

ID: 40217772
@rgb192: The links were temporarily unreachable (titles were changed). The new or modified articles must be approved by the page editor/moderator. This is the reason why some articles may be unreachable until then. The Part 1 and 2 has still the status Editor review. It should work a bit later:


But the order of reading depends on your previous experience. Reading only part 3 may be fine. Parts 1 and 2 are not Python related -- just explaining what may be needed to understand part 3 for some. Just try and write feedback there.
LVL 28

Expert Comment

ID: 40221820
The part 1 and 2 were republished (should be available now).

Author Closing Comment

ID: 40228147
thanks for pass by reference/value

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question