Solved

mutable php and python

Posted on 2014-07-20
15
475 Views
Last Modified: 2014-07-24
comparing
php
to
python

which variables are mutable

What does this mean in each language
what is the purpose in each language
0
Comment
Question by:rgb192
[X]
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
  • 7
  • 7
15 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40207783
"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
 

Author Comment

by:rgb192
ID: 40212564
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40212706
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:rgb192
ID: 40212776
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40212835
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
 

Author Comment

by:rgb192
ID: 40212947
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40213046
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
 

Author Comment

by:rgb192
ID: 40213105
public function get_a()
    {
        global $a;
        return $a;
    }

so a must be called global again in order to have value of 4?
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 40213169
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
 

Author Comment

by:rgb192
ID: 40213332
so
$this->a
is always local?


could we do
global $this->a
$this->a=3
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40213941
$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
 
LVL 34

Accepted Solution

by:
Slick812 earned 350 total points
ID: 40215341
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40215515
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
 

Author Closing Comment

by:rgb192
ID: 40215989
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
 

Author Comment

by:rgb192
ID: 40215990
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

717 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