Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

variables/objects passed by reference/value

http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28479913.html#a40215341

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.
0
rgb192
Asked:
rgb192
  • 3
  • 2
  • 2
2 Solutions
 
peprCommented:
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.
0
 
Slick812Commented:
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.
0
 
rgb192Author Commented:
If I may ask, what are you now trying to do in "python" programming (to lean it)
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/


python basics illustrated part 3

I found part 2 but permission is denied
http://www.experts-exchange.com/Programming/Languages/Scripting/Python/A_6589-Python-basics-illustrated-part-2.html

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'.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Slick812Commented:
OK, i read your comment, and I briefly looked at ocw.mit.edu 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  www.python.org, and look at their Beginners Guide at https://wiki.python.org/moin/BeginnersGuide, 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 - https://wiki.python.org/moin/BeginnersGuide/NonProgrammers,  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.
0
 
peprCommented:
@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:

http:A_5354-Python-basics-illustrated-part-1.html
http:A_6589-Python-basics-illustrated-part-2.html
http:A_7109-Python-basics-illustrated-part-3.html

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.
0
 
peprCommented:
The part 1 and 2 were republished (should be available now).
0
 
rgb192Author Commented:
thanks for pass by reference/value
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now