mutable php and python

comparing
php
to
python

which variables are mutable

What does this mean in each language
what is the purpose in each language
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
"Mutable" means "changeable."  In PHP all variables of the main scope are mutable (IMHO a great shortcoming of the language).  Inside a function, you have a separate scope that gives you the ability to make these variables immutable to the outside code.  Except for the superglobal variables and variables declared "global."  Inside a class definition, the visibility and therefore the mutability of the variables is controlled by the public, protected and private declarations, with the default (unfortunately) being public.  

After that, you might want to learn about namespaces.  Start your adventures here:
http://php.net/manual/en/language.namespaces.php
0
rgb192Author Commented:
main scope are mutable

is this the global scope

or the local scope in a method


which means that the other is not mutable?
0
Ray PaseurCommented:
By the "main scope" I mean any variable that is defined outside of a function or method.  I think that is the same as Global Scope in PHP.
http://php.net/manual/en/language.variables.scope.php
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

rgb192Author Commented:
So all global scope variables can be modified in a method and outside the class

But local scope variables can only be modified in the method?
0
Ray PaseurCommented:
Yes, that's apparently correct.  Example here, but nobody would write code like that.  The goal of variable scope is to ensure that your variables are not available for reading or writing outside a sensible context.  This reduces the risk of variable name collisions and enables two or more programmers to work on the same project simultaneously.  The existence of the global scope is an artifact of the early history of PHP, when computer science took a back seat to ease of use.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

Class Thing
{
    public function __construct()
    {
        global $a;
        $a = 3;
    }
}

$x = new Thing;
var_dump($a); // PRINTS "int(3)"

Open in new window

Also, be aware of object properties which have their own visibility according to the rules of public, protected and private.  Unlike global vs functional scope, visibility errors trigger noticeable failures, making it much easier to detect and correct malformed code.
0
rgb192Author Commented:
http://www.experts-exchange.com/viewCodeSnippet.jsp?refID=40212835&rtid=20&icsi=1

so you created an example where the local variable line9 is actually a global variable
and because line9 is now global, it is mutable?
0
Ray PaseurCommented:
Yes. Try this, for example.  But please don't think this is good coding practice -- it is not.  I'm only proving a point to address your question; this is not something you want to do!

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

Class Thing
{
    public function __construct()
    {
        global $a;
        $a = 3;
    }
    public function get_a()
    {
        global $a;
        return $a;
    }
}

$x = new Thing;
var_dump($a); // PRINTS "int(3)"

// MUTATE $a
$a = 4;
$b = $x->get_a();
var_dump($b); // PRINTS "int(4)"

Open in new window

0
rgb192Author Commented:
public function get_a()
    {
        global $a;
        return $a;
    }

so a must be called global again in order to have value of 4?
0
Ray PaseurCommented:
It must be called global in any context in which $a would be used.  In this case, it is global in the constructor, and must be called global again in the get_a() function.  Otherwise it will be given the scope of the get_a() function only.  And of course, this is different if we make it a property by referring to it as $this->a.
0
rgb192Author Commented:
so
$this->a
is always local?


could we do
global $this->a
$this->a=3
0
Ray PaseurCommented:
$this->a is not really a "local" because the object-oriented syntax does not distinguish between private, protected, and public.  They are all called $this-> something.  If you omit $this-> you get a local variable, one defined only in the scope of the method.  Unless you use the global keyword, which nobody would ever want to do inside a class method.

I have never tried it (feel free to make your own experiment) but global $this->a should be a parse error.
0
Slick812Commented:
greetings rgb192, , there seems to be much confusion here about the TERM you use in your question as -
     mutable

Your question is ill formed, because in PHP there are NO programming TERM use as mutable, so you can NOT use this statement -
      "What does this mean in each language
       what is the purpose in each language"

as there are NO reference to a mutable in the PHP language, none, it is not used.

However in python language, which has very different "thinking" and programming syntax (the code methods used) than php has to offer. In python language, the terms mutable and immutable are applied,  mutable  is used to designate that if you pass a variable (say var x) to the parameters of a function or class method then it's value CAN be changed IN the method -
x = 5
print x // outputs 5
func(x)
print x  // outputs 9, changed in the function so it's mutable

if the method designates it then the x variable can be immutable, and can NOT be valued changed.

Please understand that python represents all its data as objects (unlike php), , so it was kind of necessary to have the terms mutable and immutable

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 to the extent normally thought of, HOWEVER in code you can modify the value, BUT instead of using the previous Object in the variable (and change the value), instead it deletes the previous object and creates a NEW OBJECT, with a NEW VALUE using the same variable name like x .
Sorry but in my view there is NO comparison for php and python for programming language learning.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
It's not a term limited to any specific programming language; it's an industry concept.  Here are the dictionary definitions:
http://dictionary.reference.com/browse/mutable?s=t
http://dictionary.reference.com/browse/immutable?s=t

Here is some discussion on the concept.
http://en.wikipedia.org/wiki/Immutable_object

Here is a PHP example:
http://php.net/manual/en/class.datetimeimmutable.php
0
rgb192Author Commented:
Local and global examples are good because it is easier to change global

if the method designates it then the x variable can be immutable, and can NOT be valued changed.

Please understand that python represents all its data as objects (unlike php), , so it was kind of necessary to have the terms mutable and immutable


So now I think of python data as php objects

Thanks.
0
rgb192Author Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.