Re: [Tutor] Static Variable in Functions

2011-03-15 Thread Tom Zych
Steven D'Aprano wrote:
 Most of the common built-in Python objects are immutable:
 ...
 while a few are mutable:
 
 lists
 dicts
 sets

Also, bytearrays.

-- 
Tom Zych / freethin...@pobox.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread Yaşar Arabacı
Wow. That was a great explanation indeed. Thanks a lot. After reading 
this, I discovered something like this, and found it pretty insteresting 
indeed:



 a=[a]
 b=[a]
 a.append(c)
 b
[['a', 'c']]
 a.append(d)
 b
[['a', 'c', 'd']]

Apperantly, I can change something (which is mutable) inside  a list 
without even touching the list itself :)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread Alan Gauld

Yasar Arabaci yasar11...@gmail.com wrote


 a=[a]
 b=[a]
 a.append(c)
 b
[['a', 'c']]

Apperantly, I can change something (which is mutable) inside  a list 
without even touching the list itself :)


But the point is that you *are* touching the list.
In this case you have two names referring to the same list.
You can modify that list (because it is mutable) via either name, it
makes no difference because they both refer to the same list.

So a.append() is exactly the same operation as b.append()

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread Noah Hall
On Mon, Mar 14, 2011 at 8:56 AM, Alan Gauld alan.ga...@btinternet.com wrote:
 Yasar Arabaci yasar11...@gmail.com wrote
 Apperantly, I can change something (which is mutable) inside  a list
 without even touching the list itself :)
 But the point is that you *are* touching the list.
 In this case you have two names referring to the same list.
 You can modify that list (because it is mutable) via either name, it
 makes no difference because they both refer to the same list.

 So a.append() is exactly the same operation as b.append()


Actually, in this case it's not - a.append() is the same as b[0].append() ;)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread Marcin Wlodarczak
Alan Gauld wrote:
 Yasar Arabaci yasar11...@gmail.com wrote
 
  a=[a]
  b=[a]
  a.append(c)
  b
 [['a', 'c']]

 Apperantly, I can change something (which is mutable) inside  a list
 without even touching the list itself :)
 
 But the point is that you *are* touching the list.
 In this case you have two names referring to the same list.
 You can modify that list (because it is mutable) via either name, it
 makes no difference because they both refer to the same list.
 
 So a.append() is exactly the same operation as b.append()

In this case it is not exactly the same since he said b = [a],
not b = a. So b.append('c') would produce [['a'], 'c'].

Best,
Marcin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread ALAN GAULD
Apologies to all, I didn't notice the [] around a.

But the basic point remains that the list that he is appending 
to is the same list as b refers to, albeit indirectly. He is still 
touching the list. Although not directly modifying the list 
to which b refers, since it still only holds one member, the 
list to which a refers

 


Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/




- Original Message 
 From: Andre Engels andreeng...@gmail.com
 To: Alan Gauld alan.ga...@btinternet.com
 Cc: tutor@python.org
 Sent: Monday, 14 March, 2011 9:23:47
 Subject: Re: [Tutor] Static Variable in Functions
 
 On Mon, Mar 14, 2011 at 9:56 AM, Alan Gauld alan.ga...@btinternet.com  
wrote:
  Yasar Arabaci yasar11...@gmail.com  wrote
 
   a=[a]
b=[a]
   a.append(c)
b
  [['a', 'c']]
 
  Apperantly, I can change  something (which is mutable) inside  a list
  without even touching  the list itself :)
 
  But the point is that you *are* touching the  list.
  In this case you have two names referring to the same  list.
  You can modify that list (because it is mutable) via either name,  it
  makes no difference because they both refer to the same  list.
 
  So a.append() is exactly the same operation as  b.append()
 
 No, they are not the same list. b is (a name of) a list with  one
 element, that one element being the list (denoted by) a. That's  not
 the same as a itself.
 
 -- 
 André Engels, andreeng...@gmail.com
 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Static Variable in Functions

2011-03-13 Thread Yaşar Arabacı

Hi,

As I am starting to learn python, I follow dive into python. As I read 
object section, I came across something called class attributes and data 
attributes. Because of the reason that class attributes look and behave 
exactly like static variables in other languages (as I have used in php 
for example.) That made me think that, there should be static variables 
for functions too (or lets call them function variables?!?). Doing a 
little bit research on that, I see that there is no static variable for 
functions but we can make things behave like that as introduced here:


http://www.daniweb.com/software-development/python/threads/33025

def egg(static={count:0}):
static[count]+=1
return static[count]

print egg()
print egg()
#  1
#  2

Author of this post says that we can use mutable variables like this as 
static function variables. I was wondering what are mutable variables 
and what is rationale behind them.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-13 Thread Alan Gauld

Yasar Arabaci yasar11...@gmail.com wrote

exactly like static variables in other languages (as I have used in 
php


static in other languages usually refers to variables declared
on the memory heap rather than the stack, and therefore they
retain their value between calls. Python tends to operate at a
higher level than that and so does things in other ways.

little bit research on that, I see that there is no static variable 
for functions but we can make things behave like that as introduced 
here:


def egg(static={count:0}):
static[count]+=1
return static[count]


Yes, you can do that, see the other post earlier today for
more on how parameters work in Python.

However, you should also look at generators and the yield keyword.
These probably provide a neater way of doing what you seem to
want...

Author of this post says that we can use mutable variables like this 
as static function variables. I was wondering what are mutable 
variables and what is rationale behind them.


mutable just means modifiable.
You cannot modify a number, string or tuple for example
(you need to create new objects) but you can modify a list,
dictionary or class instance. The latter are mutable, the
former immutable. You must use an immutable object as
the key in a dictionary.

These are not Python terms they are standard computing science
terms. As such you can usually find good explanations on Wikipedia.
In general Python uses quite pure computing science concepts
and so wikipedia is a good source of background reading on
most Python concepts. (eg lambda, generator, iterator, slicing  etc)


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Static Variable in Functions

2011-03-13 Thread Steven D'Aprano

Yaşar Arabacı wrote:

Author of this post says that we can use mutable variables like this as 
static function variables. I was wondering what are mutable variables 
and what is rationale behind them.


It sounds like you are reading about Python with the mind-set of a C 
programmer. Python is not C. Python does not have variables in the same 
sense that C has. Python uses a different assignment model: instead of 
variables with a fixed type at a fixed memory location, Python's 
assignment model is that you have objects, and names in namespaces. The 
distinction is important because you can have names without objects, 
objects without names, and objects with more than one name.



A name without an object is a runtime error, not a compile-time error:

 spam  # A name without an object
Traceback (most recent call last):
  File stdin, line 1, in module
NameError: name 'spam' is not defined

But an object without a name is perfectly fine. Anonymous objects are 
very frequent in Python, so frequent that you've probably already used 
them without realising. This line creates four objects but only one name:


 t = [42, 'ham and eggs', 1.5]

The name 't' is bound to the list object. The objects 42, 'ham and eggs' 
and 1.5 (an int, a str and a float) are anonymous -- they have no names. 
But you can give them names at any time:


 breakfast = t[1]  # bind a name to the object in the list
 n = len(breakfast)  # and pass it to a function

or you can continue to use them anonymously:

 n = len(t[1])

It's not clear what variable would mean in Python unless it refers to 
the combination of a name bound to an object. I often use variable in 
that sense myself, but it's a bad habit, because it can confuse people 
who have an idea of what a variable is that is different from what 
Python does.


In Python, it is *objects* which are either mutable (changeable) or 
immutable (fixed), not names. All names are mutable in the sense that 
you can re-bind or delete them:


 x = 12345  # the name x is bound to the object 12345
 x = two  # and now the name is bound to a different object
 del x  # and now the name x is gone

But the objects themselves are inherently either mutable or immutable, 
regardless of the name. You cannot change the mutability of the object 
by changing the assignment, only by using a different object. Consider 
lists and tuples, which are both sequences of objects, but lists are 
mutable and tuples are not:


 items = [1, 2, 3]  # The list can be changed in place.
 items[2] = 4
 print(items)
[1, 2, 4]

So the variable is mutable. But if we re-bind the name to a different 
object:


 items = tuple(items)
 print(items)
(1, 2, 4)
 items[2] = 8
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'tuple' object does not support item assignment

the variable is immutable. Notice that it is the *object* that does 
not support item assignment. The *name* does not get a say about whether 
the object is mutable or not: the tuple will always be immutable, no 
matter what name it is bound to.


Most of the common built-in Python objects are immutable:

ints
floats
complex numbers
strings (both Unicode and byte strings)
tuples
bools (True and False)
None
frozensets

while a few are mutable:

lists
dicts
sets

Custom types created with the class statement are mutable, unless you 
take special efforts to make them immutable. For example, the Fraction 
class is written to be immutable:


 from fractions import Fraction as F
 f = F(1, 3)
 f
Fraction(1, 3)
 f.denominator
3
 f.denominator = 5
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: can't set attribute



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor