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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 29

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 34

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'.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 34

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 29

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 29

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

691 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