Re: [Tutor] string immutability
On Tue, Oct 25, 2011 at 12:56:56AM +0100, Alan Gauld wrote: > On 24/10/11 20:52, Johan Martinez wrote: > >Finally I figured it out ( __length__() ) thanks to ipython shell env. > > len(x) > > gets converted to x.__length__() by Python. That's actually __len__ not __length__. >>> [].__length__ Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute '__length__' >>> [].__len__ > But personally I just use dir() and help() and Google... If you use dir() a lot, you might find this useful: http://code.activestate.com/recipes/54-enhancing-dir-with-globs/ -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On 24/10/11 20:52, Johan Martinez wrote: Also, is there any doc link where I can find all the information about String object - class and instance methods. >>> help(str) or even >>> help("") For a quick list try dir() >>> dir ("") Finally I figured it out ( __length__() ) thanks to ipython shell env. len(x) gets converted to x.__length__() by Python. So you can make any object work with len() by providing a __length__() method. Similarly you can make any object into a string by providing a __str__() metjod and then str(obj) will work (as will print (obj) ) There are a bunch of these "magic" methods that you can provide to help make your objects appear more like the standard built in objects. But is there any online documentation or interactive reference like ruby-ri? There are various help browsers for Python. The one that comes with ActiveState Python for Windows is quite good IMHO and integrates with Windows Help too. But personally I just use dir() and help() and Google... -- Alan G 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] string immutability
the len() function works on lots of objects: >>> a = "this is a string" >>> len(a) 16 On Mon, Oct 24, 2011 at 3:52 PM, Johan Martinez wrote: > > > On Mon, Oct 24, 2011 at 2:32 PM, Steve Willoughby wrote: > >> On 24-Oct-11 12:17, Johan Martinez wrote: >> >>> Thanks for the replies everyone - Steve, Dave, Sander and Wayne. I >>> realized my wrong understanding/interpretation after posting the message >>> to the list, which usually happens most of the time with me! >>> >> >> That happens to most of us all the time too :) Unfortunately, with the >> lag between posting to the list and mail getting out to everyone, you'll >> probably get several replies that all say the same thing--we're not piling >> up on you, it's just a bunch of people being helpful without seeing that >> someone already answered yet. >> >> Glad we could help. Looking more into how Python variables work unlocks a >> lot of potential for all sorts of data structure operations that other >> languages require pointers to do, but are a lot easier when essentially all >> "variables" are references to objects but with the details handled behind >> the scenes for you. >> >> -- >> Steve Willoughby / st...@alchemy.com >> "A ship in harbor is safe, but that is not what ships are built for." >> PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C > > > Actually it's good to see so many replies and different methods and > explanations here. > > Also, is there any doc link where I can find all the information about > String object - class and instance methods. Google pointed me to following > two links, but that wasn't helpful for finding instance method for finding > length of a string object (rather than using 'len' function). > > - http://docs.python.org/library/string.html > - http://docs.python.org/library/stdtypes.html#string-methods > > Finally I figured it out ( __length__() ) thanks to ipython shell env. But > is there any online documentation or interactive reference like ruby-ri? > > jM. > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, Oct 24, 2011 at 2:32 PM, Steve Willoughby wrote: > On 24-Oct-11 12:17, Johan Martinez wrote: > >> Thanks for the replies everyone - Steve, Dave, Sander and Wayne. I >> realized my wrong understanding/interpretation after posting the message >> to the list, which usually happens most of the time with me! >> > > That happens to most of us all the time too :) Unfortunately, with the lag > between posting to the list and mail getting out to everyone, you'll > probably get several replies that all say the same thing--we're not piling > up on you, it's just a bunch of people being helpful without seeing that > someone already answered yet. > > Glad we could help. Looking more into how Python variables work unlocks a > lot of potential for all sorts of data structure operations that other > languages require pointers to do, but are a lot easier when essentially all > "variables" are references to objects but with the details handled behind > the scenes for you. > > -- > Steve Willoughby / st...@alchemy.com > "A ship in harbor is safe, but that is not what ships are built for." > PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C Actually it's good to see so many replies and different methods and explanations here. Also, is there any doc link where I can find all the information about String object - class and instance methods. Google pointed me to following two links, but that wasn't helpful for finding instance method for finding length of a string object (rather than using 'len' function). - http://docs.python.org/library/string.html - http://docs.python.org/library/stdtypes.html#string-methods Finally I figured it out ( __length__() ) thanks to ipython shell env. But is there any online documentation or interactive reference like ruby-ri? jM. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On 10/24/2011 03:21 PM, Johan Martinez wrote: On Mon, Oct 24, 2011 at 2:07 PM, Andreas Perstinger< andreas.perstin...@gmx.net> wrote: On 2011-10-24 20:04, Johan Martinez wrote: Hi, I am struggling to understand Python string immutability. I am able to modify Python string object after initializing/assigning it a value. s = "First" print s.__class__ print s First s = "Second" print s Second Dave, Sander and Wayne have already explained why you aren't modifying string objects in your example. With the id()-function you can also see what is happening: s = "First" id(s) 3077110080L# In CPython this is the memory address of the object # with the name 's' (in your case "First") s = "Second" id(s) 3077110304L# You see that 's' refers now to another address id("First") 3077110080L# But "First" is still on the same address as before id("Second") 3077110304L# And this proves that "Second" is at the address # which 's' refers to Bye, Andreas Great, that's really helpful Andreas. thanks, jM. Unfortunately, that trick is not guaranteed to work. The only reason that id("First") gives the same value as s="First"; id(s) is that "First" is one of the few magic values that get cached. Small non-negative integers and short strings without spaces tend to be in that category, but you can't count on it. Try a = 400 b = 400 print id(a), id(b) it'll probably print different numbers, if the value 400 isn't one of the "cached" values. Worse is that an id() can be reused once an object is garbage collected. So I could do something like: a = 600 print id(a) del a b = 400 print id(b) and it might print the same value. There are more subtle cases, but I wanted to keep it simple. An id() is guaranteed to be unique across all objects that exist at the same moment. So as long as you're comparing id's of two objects that are both present, you're fine. I'm afraid to really understand id(), you have to understand how the object model works, so you can't use id() to prove it. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On 24-Oct-11 12:17, Johan Martinez wrote: Thanks for the replies everyone - Steve, Dave, Sander and Wayne. I realized my wrong understanding/interpretation after posting the message to the list, which usually happens most of the time with me! That happens to most of us all the time too :) Unfortunately, with the lag between posting to the list and mail getting out to everyone, you'll probably get several replies that all say the same thing--we're not piling up on you, it's just a bunch of people being helpful without seeing that someone already answered yet. Glad we could help. Looking more into how Python variables work unlocks a lot of potential for all sorts of data structure operations that other languages require pointers to do, but are a lot easier when essentially all "variables" are references to objects but with the details handled behind the scenes for you. -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
What you did here is just re-assigning s. Try slicing s and then assign a new value to the slice. s[2] would return 'r' now try to to set s[2] to another value to understand immutability. Hope it helps. On 24 Oct 2011 19:06, "Johan Martinez" wrote: > Hi, > > I am struggling to understand Python string immutability. I am able to > modify Python string object after initializing/assigning it a value. So how > does immutability work? I am not following it. Sorry for really stupid > question. Any help? > > > > >>> s = "First" > >>> print s.__class__ > > >>> print s > First > >>> s = "Second" > >>> print s > Second > >>> > > > > jM. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, Oct 24, 2011 at 2:07 PM, Andreas Perstinger < andreas.perstin...@gmx.net> wrote: > On 2011-10-24 20:04, Johan Martinez wrote: > >> Hi, >> >> I am struggling to understand Python string immutability. I am able to >> modify Python string object after initializing/assigning it a value. >> >> s = "First" > print s.__class__ > >> >>> print s > First >> >>> s = "Second" > print s > Second >> > > Dave, Sander and Wayne have already explained why you aren't modifying > string objects in your example. > With the id()-function you can also see what is happening: > > >>> s = "First" > >>> id(s) > 3077110080L# In CPython this is the memory address of the object > # with the name 's' (in your case "First") > >>> s = "Second" > >>> id(s) > 3077110304L# You see that 's' refers now to another address > >>> id("First") > 3077110080L# But "First" is still on the same address as before > >>> id("Second") > 3077110304L# And this proves that "Second" is at the address > # which 's' refers to > > Bye, Andreas > Great, that's really helpful Andreas. thanks, jM. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, Oct 24, 2011 at 1:52 PM, Wayne Werner wrote: > On Mon, Oct 24, 2011 at 1:04 PM, Johan Martinez wrote: > >> Hi, >> >> I am struggling to understand Python string immutability. I am able to >> modify Python string object after initializing/assigning it a value. So how >> does immutability work? I am not following it. Sorry for really stupid >> question. Any help? >> >> >> >> >>> s = "First" >> >>> print s.__class__ >> >> >>> print s >> First >> >>> s = "Second" >> > > This is not actually modifying the string object. Unlike most other > programming languages where a variable refers to an actual location in > memory (usually), in Python the variable names the actual value. > > So when you do s = "First" then you are telling python that you want to be > able to refer to the string "First" by the name/variable s. > > When you execute s="Second" you are now telling python that instead of > referring to "First" you want the name 's' to refer to the string "Second". > > If you try to modify the actual value of the string, you will raise an > exception: > > >>> s = "First" > >>> s[0] = "T" > Traceback (most recent call last): > File "", line 1, in > TypeError: 'str' object does not support item assignment > > HTH, > Wayne > Thanks for the replies everyone - Steve, Dave, Sander and Wayne. I realized my wrong understanding/interpretation after posting the message to the list, which usually happens most of the time with me! jM. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On 2011-10-24 20:04, Johan Martinez wrote: Hi, I am struggling to understand Python string immutability. I am able to modify Python string object after initializing/assigning it a value. s = "First" print s.__class__ print s First s = "Second" print s Second Dave, Sander and Wayne have already explained why you aren't modifying string objects in your example. With the id()-function you can also see what is happening: >>> s = "First" >>> id(s) 3077110080L# In CPython this is the memory address of the object # with the name 's' (in your case "First") >>> s = "Second" >>> id(s) 3077110304L# You see that 's' refers now to another address >>> id("First") 3077110080L# But "First" is still on the same address as before >>> id("Second") 3077110304L# And this proves that "Second" is at the address # which 's' refers to Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, Oct 24, 2011 at 01:04:20PM -0500, Johan Martinez wrote: > Hi, > > I am struggling to understand Python string immutability. I am able to > modify Python string object after initializing/assigning it a value. So how > does immutability work? I am not following it. Sorry for really stupid > question. Any help? No, you're actualy not. > >>> s = "First" > >>> print s > First At this point, you have created a string object with the value "First" and put that into the variable "s" (which is glossing over a detail about how Python variables realy work, but that's another topic). > >>> s = "Second" > >>> print s > Second Now you created a new string object with the value "Second" and stored THAT into s, replacing the original object, which is now lost. -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, Oct 24, 2011 at 1:04 PM, Johan Martinez wrote: > Hi, > > I am struggling to understand Python string immutability. I am able to > modify Python string object after initializing/assigning it a value. So how > does immutability work? I am not following it. Sorry for really stupid > question. Any help? > > > > >>> s = "First" > >>> print s.__class__ > > >>> print s > First > >>> s = "Second" > This is not actually modifying the string object. Unlike most other programming languages where a variable refers to an actual location in memory (usually), in Python the variable names the actual value. So when you do s = "First" then you are telling python that you want to be able to refer to the string "First" by the name/variable s. When you execute s="Second" you are now telling python that instead of referring to "First" you want the name 's' to refer to the string "Second". If you try to modify the actual value of the string, you will raise an exception: >>> s = "First" >>> s[0] = "T" Traceback (most recent call last): File "", line 1, in TypeError: 'str' object does not support item assignment HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On Mon, 24 Oct 2011, 20:04:20 CEST, Johan Martinez wrote: > I am struggling to understand Python string immutability. I am able to > modify Python string object after initializing/assigning it a value. So > how does immutability work? I am not following it. Sorry for really > stupid question. Any help? Mutibility means changinging the object (string) in place. What you are doing below is creating a new string and asigning it to a variable. > > > > > > s = "First" > > > > print s.__class__ > > > > > print s > First > > > > s = "Second" > > > > print s > Second > > > > > > If the object s reffernces is mutable you should be able to do: s[0] = 'x' Try it and see what happen. Then also try this with a list of strings s = ['f', 'i', 'r', 's', 't'] s[0] = 'x' Greets sander___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string immutability
On 10/24/2011 02:04 PM, Johan Martinez wrote: Hi, I am struggling to understand Python string immutability. I am able to modify Python string object after initializing/assigning it a value. So how does immutability work? I am not following it. Sorry for really stupid question. Any help? You're confusing attributes and objects. When you say s = "First" two distinct things happen. An immutable object of type str is created, with an immutable value "First". And then the attribute s is bound to that object. s is not immutable at all. Then when you do s = "Second" you are creating a totally different immutable object, which s is now bound to. And behind the scenes, the first object is garbage collected. We all confuse this by referring to "variables," but they are not the same as "variables" in most other languages. They never have a value, they just refer to an object. And if you dot = s, you're not copying a value at all. You're just saying that t and s now are bound to (refer to) the same object. If the object is immutable, then you don't need the distinction. But if you mute the object, as opposed to creating a new one, both the attributes are still bound to the same object. Top-level (global) variables are attributes of the module. Local variables are attributes of the function. And instance variables (an incorrect term) are attributes of an instance, frequently referred to asobj.inst Going from the other end, an object may be bound to one place, two places, or many. And when it's bound to nothing, it gets garbage collected (sometimes ref-counted, but that disctinction refers to a particular implementation, not to the language Python). Those "places" I'm referring to may be attributes, so we see it as having "a name", but it may be bound to something without an explicit name, such as an element of a list. Ints, floats, and strings are immutable. So I guess the simplest object that's mutable is a list. You can modify the 3rd item in a list without affecting any of the "variables" that are bound to it. But when you use any of those variables, it'll appear to have a "new value." list1 = [3, 0, 44] list2= list1 list3 = [10, 12, 15, 22] list1[2] = 45 #mutates the list object bound to list1 #but list2 is bound to the same object print list2 #will now be [3, 0, 45] list2 = list3 #doesn't mutate any objects, it simply rebinds list2 from the first list object to the second one. HTH -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] string immutability
Hi, I am struggling to understand Python string immutability. I am able to modify Python string object after initializing/assigning it a value. So how does immutability work? I am not following it. Sorry for really stupid question. Any help? >>> s = "First" >>> print s.__class__ >>> print s First >>> s = "Second" >>> print s Second >>> jM. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor