I thought I'd 'got' globals but...

2006-07-06 Thread meridian
I thought I had 'got' globals but this one seems strange.
I want my example function 'doIt' to use and optionally modify a module
variable 'gname', so I declare 'global gname' in the function, but when
modified it doesn't stay modified.

gname = 'Sue'
def doIt(name = gname):
global gname
gname = name
print 'doIt name', name, 'gname', gname

print 'start gname', gname
doIt()
doIt(name='Lisa')
doIt()
print 'finish gname', gname

gives...
 start gname Sue
 doIt name Sue gname Sue
 doIt name Lisa gname Lisa
 doIt name Sue gname Sue
 finish gname Sue

The variable gname has reverted back to value 'Sue'

Curiously though, without the third doIt() call, it works...
print 'start gname', gname
doIt()
doIt(name='Lisa')
#doIt()
print 'finish gname', gname

gives...
 start gname Sue
 doIt name Sue gname Sue
 doIt name Lisa gname Lisa
 finish gname Lisa

The variable gname has been modified to 'Lisa'

Any ideas how I can make the 'Lisa' assignment permanent forever in 2nd
doIt?   Thanks

(Note. Contrived example substitutes for a web-type app, where, if the
page is called without arguments then it displays the global defaults.
If the page is called with form arguments then it should be able to
change the global defaults)

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


Re: I thought I'd 'got' globals but...

2006-07-06 Thread meridian

Bruno Desthuilliers wrote:
> def doIt(name=None):
>   global gname
>   if name is None:
> name = gname
>   else:
> gname = name
> 

Thanks Bruno, works a treat...

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


Re: I thought I'd 'got' globals but...

2006-07-06 Thread meridian

Bruno Desthuilliers wrote:
> meridian wrote:
> > Bruno Desthuilliers wrote:
> >
> >>def doIt(name=None):
> >>  global gname
> >>  if name is None:
> >>name = gname
> >>  else:
> >>gname = name
> >>
> >
> >
> > Thanks Bruno, works a treat...
> >
> But still very probably a bad idea.
>
Ok, my curiosity is pique'd - is there a better way to hold my
app/module globals?
I must say I'd prefer not to write global  in every function, for
every global var I need.
Maybe there's a simpler way to structure?... by a global class or
something?
Cheers
Steve

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


Re: I thought I'd 'got' globals but...

2006-07-06 Thread meridian

You mentioned earlier that
"Modifying globals from within a function is usually a very bad idea."

Most of my app consists of functions or class/object functions, that's
all I do in OOP.
Did you mean that modifying globals from anywhere is bad? or globals
are bad? or don't code using methods/functions?  not sure...

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


Re: I thought I'd 'got' globals but...

2006-07-07 Thread meridian
Thanks Bruno. Not only do you give solutions to my problem but also
throw in great MVC tutorials too.
You're a gent.

I'm using
controller -> A CherryPy app
views -> Cheetah Templating for the html & data
model -> mostly SQLite DB using pysqlite
also Config

It's only a desk-web app for single-user, so no concurrency issues.
However, taking on board your advice to avoid globals, I've now
realised that the info that I need to retain between pages can simply
be session information (for the only user) that can be initialised at
startup from the config.
So I'm looking at just storing it all in the cherrypy.session ram vs
using python globals.
Thanks again & Regards
Steve

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


slices - handy summary

2006-12-13 Thread meridian
If, like me, you're always forgetting which way around your list/seq
slices need to go then worry no more. Just put my handy "slice
lookupper" (TM) ) on a (large!) PostIt beside your screen and, Hey
Presto! no more tediously typing a 1-9 seq into your interpreter and
then getting a slice just to check what you get.. (Yes you. You know
you do that !) ...Cheers Steve

x = '0123456789'

x[-10: ] 0123456789 x[  0: ]
x[ -9: ] 123456789  x[  1: ]
x[ -8: ] 23456789   x[  2: ]
x[ -7: ] 3456789x[  3: ]
x[ -6: ] 456789 x[  4: ]
x[ -5: ] 56789  x[  5: ]
x[ -4: ] 6789   x[  6: ]
x[ -3: ] 789x[  7: ]
x[ -2: ] 89 x[  8: ]
x[ -1: ] 9  x[  9: ]

x[ :-9 ] 0  x[ :1  ]
x[ :-8 ] 01 x[ :2  ]
x[ :-7 ] 012x[ :3  ]
x[ :-6 ] 0123   x[ :4  ]
x[ :-5 ] 01234  x[ :5  ]
x[ :-4 ] 012345 x[ :6  ]
x[ :-3 ] 0123456x[ :7  ]
x[ :-2 ] 01234567   x[ :8  ]
x[ :-1 ] 012345678  x[ :9  ]
 0123456789 x[ :10 ]

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