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

2006-07-09 Thread Bruno Desthuilliers
Luis M. González a écrit : (snip) > OK, so I should include the global only if I plan to modify it. > Otherwise, I don't need to include it. Am I right? s/modify/rebind/ -- http://mail.python.org/mailman/listinfo/python-list

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

2006-07-09 Thread Bruno Desthuilliers
Luis M. González a écrit : > Bruno Desthuilliers wrote: > >>def doIt(name=None): >> global gname >> if name is None: >>name = gname >> else: >>gname = name >> > > > Sorry for this very basic question, but I don't understand why I should > add the global into the function body before u

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

2006-07-07 Thread Luis M. González
Markus Wankus wrote: > On Fri, 07 Jul 2006 19:41:36 -0400, Luis M. González <[EMAIL PROTECTED]> > wrote: > . > . > > OK, so I should include the global only if I plan to modify it. > > Otherwise, I don't need to include it. Am I right? > > > > Correct. Globals are always available to read from.

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

2006-07-07 Thread Markus Wankus
On Fri, 07 Jul 2006 19:41:36 -0400, Luis M. González <[EMAIL PROTECTED]> wrote: . . > OK, so I should include the global only if I plan to modify it. > Otherwise, I don't need to include it. Am I right? > Correct. Globals are always available to read from. You need to declare them if you wan

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

2006-07-07 Thread nate
> OK, so I should include the global only if I plan to modify it. > Otherwise, I don't need to include it. Am I right? I guess you could say that's true. I'm hardly an expert so I couldn't say there aren't other potential ramifications. (anyone?) But, as a rule I would declare the global varia

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

2006-07-07 Thread Luis M. González
nate wrote: > try this: > > gname = 'nate' > def test(): >gname = 'amy' >print gname > > test() > print gname > > outputs: > 'amy' > 'nate' > > whereas this: > gname = 'nate' > def test(): >global gname >gname = 'amy' >print gname > > test() > print gname > > outputs: > 'amy' >

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

2006-07-07 Thread nate
try this: gname = 'nate' def test(): gname = 'amy' print gname test() print gname outputs: 'amy' 'nate' whereas this: gname = 'nate' def test(): global gname gname = 'amy' print gname test() print gname outputs: 'amy' 'amy' Luis M. González wrote: > Bruno Desthuilliers wrote:

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

2006-07-07 Thread Luis M. González
Bruno Desthuilliers wrote: > > def doIt(name=None): > global gname > if name is None: > name = gname > else: > gname = name > Sorry for this very basic question, but I don't understand why I should add the global into the function body before using it. This function works even if I

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

2006-07-07 Thread Bruno Desthuilliers
meridian wrote: > Thanks Bruno. Not only do you give solutions to my problem but also > throw in great MVC tutorials too. > You're a gent. (blush) > I'm using > controller -> A CherryPy app > views -> Cheetah Templating for the html & data > model -> mostly SQLite DB using pysqlite > also Config

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

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

2006-07-06 Thread Bruno Desthuilliers
meridian a écrit : > 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? Yes, definitively. Ev

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

2006-07-06 Thread Bruno Desthuilliers
meridian a écrit : > 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 pr

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/funct

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, m

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

2006-07-06 Thread Bruno Desthuilliers
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. -- bruno desthuilliers python -c "print '@'.join(['.'.join(

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 Bruno Desthuilliers
meridian wrote: > 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 = g

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 gn