Re: mutate dictionary or list

2010-09-08 Thread Baba
On 7 sep, 16:46, Ben Finney ben+pyt...@benfinney.id.au wrote:
 de...@web.de writes:
  Objects can be mutable or immutable. For example, in Python, integers,
  strings, floats and tuples are immutable. That means that you can't
  change their value.

 Yes. Importantly, wherever you see code that you *think* is changing the
 value of an immutable object, you're thinking incorrectly. (There's no
 shame in that; other languages give us preconceptions that can be hard
 to shake off.)

 The only way to get a different value from an integer object is to ask
 Python for a different integer object; the original is unchanged. The
 same goes for tuples, strings, and all the other immutable types.

  Mutable objects OTOH can be changed.

 […]

 Some good articles to explain Python's object model:

      URL:http://effbot.org/zone/python-objects.htm
      
 URL:http://docs.python.org/reference/datamodel.html#objects-values-and-types

 --
  \             “We can't depend for the long run on distinguishing one |
   `\         bitstream from another in order to figure out which rules |
 _o__)               apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 |
 Ben Finney

Thanks all for feedback!
Baba
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutate dictionary or list

2010-09-08 Thread Paul McGuire
On Sep 7, 7:05 am, Baba raoul...@gmail.com wrote:
 Hi

 I am working on an exercise which requires me to write a funtion that
 will check if a given word can be found in a given dictionary (the
 hand).

 def is_valid_word(word, hand, word_list):
     
     Returns True if word is in the word_list and is entirely
     composed of letters in the hand. Otherwise, returns False.
     Does not mutate hand or word_list.

 I don't understand this part: Does not mutate hand or word_list


I would re-read your exercise description.  hand is *not* a
dictionary, but is most likely a list of individual letters.
word_list too is probably *not* a dictionary, but a list of valid
words (although this does bear a resemblance to what people in
everyday life call a dictionary).  Where did you get the idea that
there was a dictionary in this problem?

The Does not mutate hand or word_list. is a constraint that you are
not allowed to update the hand or word_list arguments.  For instance,
you must not call word_list.sort() in order to search for the given
word using some sort of binary search.  You must not determine if all
the letters in word come from hand by modifying the hand list (like
dropping letters from hand as they are found in word).  There are ways
to copy arguments if you use a destructive process on their contents,
so that the original stays unmodified - but that sounds like part of
the exercise for you to learn about.

-- Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutate dictionary or list

2010-09-07 Thread Xavier Ho
On 7 September 2010 22:05, Baba raoul...@gmail.com wrote:


 It would be great if someone could give me a brief explanantion of the
 mutation concept.


In this case, to mutate is to change. If you must not mutate the list, you
must not change it.

In another words, reading from the list is fine. Writing to it is not.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutate dictionary or list

2010-09-07 Thread Bruno Desthuilliers

Baba a écrit :

Hi

I am working on an exercise which requires me to write a funtion that
will check if a given word can be found in a given dictionary (the
hand).

def is_valid_word(word, hand, word_list):

Returns True if word is in the word_list and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or word_list.

I don't understand this part: Does not mutate hand or word_list


Everything in Python is an object. A few objects are immutable (ints, 
strings, tuples...), ie you there's no way to modify the state of the 
object in place. Most objects are mutable, and this obviously includes 
lists and dicts (you can add / remove / replace list or dicts elements).


Now the point is that when passing an object as argument to a function, 
you don't pass a copy of the object but the object itself, so if the 
object is mutable, you can mutate if from within the function.


A simple (and really dumb) example being worth a thousand words:

# mutate.py

def update_dict(dic, key, value):
print in update_dic : dic id is %s % id(dic)
dic[key] = value

def main():
   dic = dict(a=1, b=2)
   lst = [1, 2, 3]

   print in main : dic id is %s % id(dic)
   print dic : %s % dic
   print calling update_dict
   update_dict(dic, c, 3)
   print after update_dict
   print in main : dic id is %s % id(dic)
   print dic : %s % dic

if __name__ == '__main__':
main()




I know that a ditionary is unordered. How Can i however avoid
'acidental' mutation?


This has nothing to do with dicts being ordered or not. And there's NO 
accidental mutation - you have to explicitely call a method or 
operator that mutate the dict.



--
http://mail.python.org/mailman/listinfo/python-list


Re: mutate dictionary or list

2010-09-07 Thread deets
Baba raoul...@gmail.com writes:

 Hi

 I am working on an exercise which requires me to write a funtion that
 will check if a given word can be found in a given dictionary (the
 hand).

 def is_valid_word(word, hand, word_list):
 
 Returns True if word is in the word_list and is entirely
 composed of letters in the hand. Otherwise, returns False.
 Does not mutate hand or word_list.

 I don't understand this part: Does not mutate hand or word_list

 I tried to google python mutate list input but to no avail

 It would be great if someone could give me a brief explanantion of the
 mutation concept.

Objects can be mutable or immutable. For example, in Python, integers,
strings, floats and tuples are immutable. That means that you can't
change their value.

Mutable objects OTOH can be changed. For example, a list is mutable:

 l = [foo]
 l.append(bar) # mutating method
 print l #- ['foo', 'bar']

That's all there is to it. So for the example at hand, don't use
anything that mutates the passed arguments. E.g, if word_list really is
a list, and for faster lookup of word, you want to sort it, you are
not allowed to do this:

  word_list.sort() # mutating!!

Instead, you need to do

  new_word_list = sorted(word_list) # creates a *copy* of word_list,
  which is sorted.

Actually, you can also try  use the module copy's deepcopy-function
to ensure that you don't mutate the passed objects.

Please not that this is *not* a mutating operation:

 l = [1, 2]
 h = l
 l = [3, 4]
 print h #- [1, 2]

The original list in l is still preserved un-modified, and referenced by
the name h. Just binding a different object to an existing name doesn't
change anything about the old object referenced by the name.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutate dictionary or list

2010-09-07 Thread Ben Finney
de...@web.de writes:

 Objects can be mutable or immutable. For example, in Python, integers,
 strings, floats and tuples are immutable. That means that you can't
 change their value.

Yes. Importantly, wherever you see code that you *think* is changing the
value of an immutable object, you're thinking incorrectly. (There's no
shame in that; other languages give us preconceptions that can be hard
to shake off.)

The only way to get a different value from an integer object is to ask
Python for a different integer object; the original is unchanged. The
same goes for tuples, strings, and all the other immutable types.

 Mutable objects OTOH can be changed.
[…]

Some good articles to explain Python's object model:

 URL:http://effbot.org/zone/python-objects.htm
 
URL:http://docs.python.org/reference/datamodel.html#objects-values-and-types

-- 
 \ “We can't depend for the long run on distinguishing one |
  `\ bitstream from another in order to figure out which rules |
_o__)   apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list