Solved

variables/objects passed by reference/value

Posted on 2014-07-23
7
235 Views
Last Modified: 2014-07-29
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
Comment
Question by:rgb192
  • 3
  • 2
  • 2
7 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 250 total points
Comment Utility
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
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
 

Author Comment

by:rgb192
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 250 total points
Comment Utility
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
 
LVL 28

Expert Comment

by:pepr
Comment Utility
@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
 
LVL 28

Expert Comment

by:pepr
Comment Utility
The part 1 and 2 were republished (should be available now).
0
 

Author Closing Comment

by:rgb192
Comment Utility
thanks for pass by reference/value
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now