Re: [Tutor] Operating in Place

2010-09-28 Thread Steven D'Aprano
On Wed, 29 Sep 2010 06:12:20 am Shashwat Anand wrote:
> On Wed, Sep 29, 2010 at 1:33 AM, Corey Richardson  
wrote:
> >  Hello tutors.
> >
> > I hate doing this:
> >string = string.lower()
> >
> > Is there a way to do it without the "string =" part? Thanks.
>
> 1. string is a module which is deprecated. You should probably use
> str or s in your example.

Actually, in this case Corey is not using the string module, but is 
using string as the name of a variable.



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


Re: [Tutor] Operating in Place

2010-09-28 Thread Steven D'Aprano
On Wed, 29 Sep 2010 06:03:21 am Corey Richardson wrote:
>   Hello tutors.
>
> I hate doing this:
>  string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.

No, strings are immutable. Once they're created, they cannot be changed.

This is no different from:

x = 42
x = x/2  # or x /= 2 if you prefer

instead of:

x = 42
x/2


As an alternative you could look at the UserString module, and the 
MutableString class it includes, but MutableString has some serious 
limitations and is not really meant for serious work. But it might do 
the job you want.



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


Re: [Tutor] Operating in Place

2010-09-28 Thread Rance Hall
On Tue, Sep 28, 2010 at 3:03 PM, Corey Richardson  wrote:
>  Hello tutors.
>
> I hate doing this:
>            string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.
>

I suppose the best answer is it depends on what you are doing with
string after you do string.lower()

you can use the string.lower() directly in IF statements and such without worry


a = "TEST"

if a.lower() == "test":
   do stuff

works for me.even on the new 3.1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Operating in Place

2010-09-28 Thread Steve Willoughby

On 28-Sep-10 13:03, Corey Richardson wrote:

I hate doing this:
string = string.lower()

Is there a way to do it without the "string =" part? Thanks.


Depends on the class.  In this specific case, string objects are 
immutable (for some good reasons which are beyond the immediate point), 
so once created, they can't be changed.  They can, of course, be used to 
create new strings, which is what string.lower() is doing.  And that new 
string is then given the name "string" again, replacing the old string 
object (which may still have other names referencing it elsewhere).


If you were wanting to modify a mutable object in-place, Python would be 
happy to oblige.  But not strings.


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


Re: [Tutor] Operating in Place

2010-09-28 Thread Wayne Werner
On Tue, Sep 28, 2010 at 3:03 PM, Corey Richardson  wrote:

>  Hello tutors.
>
> I hate doing this:
>string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.
>

Not with a string. I suppose if you had your own class you could create
something, but you'd be doing the same thing behind the scenes.

Is there any particular reason why you don't like doing it? It's explicit
and easy to understand - saying that you want the lowercase version of the
string, but you don't care about the original string.

At least that's my take on it,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Operating in Place

2010-09-28 Thread Shashwat Anand
On Wed, Sep 29, 2010 at 1:33 AM, Corey Richardson  wrote:

>  Hello tutors.
>
> I hate doing this:
>string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.
>

1. string is a module which is deprecated. You should probably use str or s
in your example.
2. strings in python are immutable. If you need to change something, you
need to create a new string.


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



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


[Tutor] Operating in Place

2010-09-28 Thread Corey Richardson

 Hello tutors.

I hate doing this:
string = string.lower()

Is there a way to do it without the "string =" part? Thanks.

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