Re: Default scope of variables

2013-07-10 Thread Frank Millman
"Ian Kelly" wrote in message news:calwzidk2+b5bym5b+xvtoz8lheyvhcos4v58f8z2o1jb6sa...@mail.gmail.com... > On Tue, Jul 9, 2013 at 11:54 PM, Frank Millman wrote: >> You had me worried there for a moment, as that is obviously an error. >> >> Then I checked my actual code, and I find that I mis-tra

Re: Default scope of variables

2013-07-10 Thread Ethan Furman
On 07/09/2013 10:54 PM, Frank Millman wrote: "Ian Kelly" wrote in message news:calwzidnf3obe0enf3xthlj5a40k8hxvthveipecq8+34zxy...@mail.gmail.com... On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: You could also do it like this: def updating(self): self.transaction_active

Re: Default scope of variables

2013-07-10 Thread Ian Kelly
On Tue, Jul 9, 2013 at 11:54 PM, Frank Millman wrote: > You had me worried there for a moment, as that is obviously an error. > > Then I checked my actual code, and I find that I mis-transcribed it. It > actually looks like this - > > with db_session as conn: > db_session.transaction_a

Re: Default scope of variables

2013-07-09 Thread Frank Millman
"Ian Kelly" wrote in message news:calwzidnf3obe0enf3xthlj5a40k8hxvthveipecq8+34zxy...@mail.gmail.com... > On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: >> You could also do it like this: >> >> def updating(self): >> self.transaction_active = True >> return self > > Ye

Re: Default scope of variables

2013-07-09 Thread Ethan Furman
On 07/09/2013 11:41 AM, Ian Kelly wrote: On Tue, Jul 9, 2013 at 11:23 AM, Ethan Furman wrote: On 07/09/2013 09:44 AM, Ian Kelly wrote: On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: You could also do it like this: def updating(self): self.transaction_active = True

Re: Default scope of variables

2013-07-09 Thread Ian Kelly
On Tue, Jul 9, 2013 at 11:23 AM, Ethan Furman wrote: > On 07/09/2013 09:44 AM, Ian Kelly wrote: >> >> On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: >>> >>> You could also do it like this: >>> >>> def updating(self): >>> self.transaction_active = True >>> return self

Re: Default scope of variables

2013-07-09 Thread Ethan Furman
On 07/09/2013 09:44 AM, Ian Kelly wrote: On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: You could also do it like this: def updating(self): self.transaction_active = True return self Yes, that would be simpler. I was all set to point out why this doesn't work, a

Re: Default scope of variables

2013-07-09 Thread Ian Kelly
On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: > You could also do it like this: > > def updating(self): > self.transaction_active = True > return self Yes, that would be simpler. I was all set to point out why this doesn't work, and then I noticed that the location of

Re: Default scope of variables

2013-07-09 Thread Ethan Furman
On 07/09/2013 01:38 AM, Frank Millman wrote: "Ian Kelly" wrote in message news:CALwzid=fzgjpebifx1stdbkh8iwltwggwwptphz1ykyg+05...@mail.gmail.com... On Tue, Jul 9, 2013 at 1:35 AM, Frank Millman wrote: When any of them need any database access, whether for reading or for updating, they execu

Re: Default scope of variables

2013-07-09 Thread Frank Millman
"Ian Kelly" wrote in message news:CALwzid=fzgjpebifx1stdbkh8iwltwggwwptphz1ykyg+05...@mail.gmail.com... > On Tue, Jul 9, 2013 at 1:35 AM, Frank Millman wrote: >> When any of them need any database access, whether for reading or for >> updating, they execute the following - >> >> with db_ses

Re: Default scope of variables

2013-07-09 Thread Frank Millman
"Chris Angelico" wrote in message news:captjjmr4mr0qcgwqxwyvdcz55nuav79vbtt8bjndsdvhrkq...@mail.gmail.com... > On Tue, Jul 9, 2013 at 5:35 PM, Frank Millman wrote: >> I have been following this sub-thread with interest, as it resonates with >> what I am doing in my project. > > Just FYI, none o

Re: Default scope of variables

2013-07-09 Thread Ian Kelly
On Tue, Jul 9, 2013 at 1:35 AM, Frank Millman wrote: > When any of them need any database access, whether for reading or for > updating, they execute the following - > > with db_session as conn: > conn.transaction_active = True # this line must be added if > updating > conn.cu

Re: Default scope of variables

2013-07-09 Thread Chris Angelico
On Tue, Jul 9, 2013 at 5:35 PM, Frank Millman wrote: > I have been following this sub-thread with interest, as it resonates with > what I am doing in my project. Just FYI, none of my own code will help you as it's all using libpqxx, but the docs for the library itself are around if you want them

Re: Default scope of variables

2013-07-09 Thread Frank Millman
"Chris Angelico" wrote in message news:captjjmqkmfd4-jpugr-vubub6ribv6k_mwnxc_u3cvabr_w...@mail.gmail.com... > On Tue, Jul 9, 2013 at 4:08 PM, alex23 wrote: >> On 9/07/2013 3:07 PM, Chris Angelico wrote: >>> >>> The subtransactions are NOT concepted as separate transactions. They >>> are effect

Re: Default scope of variables

2013-07-08 Thread Chris Angelico
On Tue, Jul 9, 2013 at 4:08 PM, alex23 wrote: > On 9/07/2013 3:07 PM, Chris Angelico wrote: >> >> The subtransactions are NOT concepted as separate transactions. They >> are effectively the database equivalent of a try/except block. > > > Sorry, I assumed each nested query was somehow related to t

Re: Default scope of variables

2013-07-08 Thread alex23
On 9/07/2013 3:07 PM, Chris Angelico wrote: The subtransactions are NOT concepted as separate transactions. They are effectively the database equivalent of a try/except block. Sorry, I assumed each nested query was somehow related to the prior one. In which case, I'd probably go with Ethan's su

Re: Default scope of variables

2013-07-08 Thread Chris Angelico
On Tue, Jul 9, 2013 at 2:52 PM, alex23 wrote: > with new_transaction(conn) as folder_tran: > folder_tran.query("blah") > with folder_tran.subtransaction() as file_tran: > file_tran.query("blah") > with file_tran.subtransaction() as type_tran: > type_tran.query("

Re: Default scope of variables

2013-07-08 Thread alex23
On 7/07/2013 11:43 PM, Chris Angelico wrote: Yep. There's a problem, though, when you bring in subtransactions. The logic wants to be like this: with new_transaction(conn) as tran: tran.query("blah") with tran.subtransaction() as tran: tran.query("blah") with tran.sub

Re: Default scope of variables

2013-07-08 Thread Joshua Landau
On 4 July 2013 05:36, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau > wrote: >> That said, I'm not too convinced. Personally, the proper way to do >> what you are talking about is creating a new closure. Like: >> >> for i in range(100): >> with new_scope(): >>

Re: Default scope of variables

2013-07-08 Thread Joshua Landau
On 8 July 2013 12:54, Neil Cerutti wrote: > On 2013-07-07, Steven D'Aprano wrote: >> On Fri, 05 Jul 2013 13:24:43 +, Neil Cerutti wrote: >> >>> for x in range(4): >>>print(x) >>> print(x) # Vader NOoOO!!! >> >> That loops do *not* introduce a new scope is a feature, not a bug. It

Re: Default scope of variables

2013-07-08 Thread Neil Cerutti
On 2013-07-07, Steven D'Aprano wrote: > On Fri, 05 Jul 2013 13:24:43 +, Neil Cerutti wrote: > >> for x in range(4): >>print(x) >> print(x) # Vader NOoOO!!! > > That loops do *not* introduce a new scope is a feature, not a bug. It is > *really* useful to be able to use the value of

Re: Default scope of variables

2013-07-07 Thread Chris Angelico
On Mon, Jul 8, 2013 at 3:00 PM, Steven D'Aprano wrote: > On Mon, 08 Jul 2013 13:11:37 +1000, Chris Angelico wrote: >> It's not something to be solved by the language, but it's often >> something to be solved by the program's design. Two lines of code that >> achieve the same goal should normally l

Re: Default scope of variables

2013-07-07 Thread Steven D'Aprano
On Mon, 08 Jul 2013 13:11:37 +1000, Chris Angelico wrote: > On Mon, Jul 8, 2013 at 12:23 PM, Steven D'Aprano > wrote: >> On Mon, 08 Jul 2013 10:48:03 +1000, Chris Angelico wrote: [...] >>> That means that I, as programmer, have to keep track of the nesting >>> level of subtransactions. Extremely

Re: Default scope of variables

2013-07-07 Thread Chris Angelico
On Mon, Jul 8, 2013 at 12:23 PM, Steven D'Aprano wrote: > On Mon, 08 Jul 2013 10:48:03 +1000, Chris Angelico wrote: > [...] >> That means that I, as programmer, have to keep track of the nesting >> level of subtransactions. Extremely ugly. A line of code can't be moved >> around without first chec

Re: Default scope of variables

2013-07-07 Thread Steven D'Aprano
On Mon, 08 Jul 2013 10:48:03 +1000, Chris Angelico wrote: [...] > That means that I, as programmer, have to keep track of the nesting > level of subtransactions. Extremely ugly. A line of code can't be moved > around without first checking which transaction object to work with. I feel your pain, b

Re: Default scope of variables

2013-07-07 Thread Chris Angelico
On Mon, Jul 8, 2013 at 2:52 AM, Ethan Furman wrote: > On 07/07/2013 06:43 AM, Chris Angelico wrote: >> >> On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner >> wrote: >>> >>> Which you would then use like: >>> >>> >>> conn = create_conn() >>> with new_transaction(conn) as tran: >>> rows_affected

Re: Default scope of variables

2013-07-07 Thread Chris Angelico
On Mon, Jul 8, 2013 at 2:03 AM, Steven D'Aprano wrote: > If you need more than two levels, you probably ought to re-design your > code to be less confusing, otherwise you may be able to use ChainMap to > emulate any number of nested scopes. The subtransactions are primarily to represent the datab

Re: Default scope of variables

2013-07-07 Thread Ethan Furman
On 07/07/2013 06:43 AM, Chris Angelico wrote: The 'with' statement doesn't allow this. I would need to use some kind of magic to rebind the old transaction to the name, or else use a list that gets magically populated: with new_transaction(conn) as tran: tran[-1].query("blah") with su

Re: Default scope of variables

2013-07-07 Thread Ethan Furman
On 07/07/2013 06:43 AM, Chris Angelico wrote: On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner wrote: Which you would then use like: conn = create_conn() with new_transaction(conn) as tran: rows_affected = do_query_stuff(tran) if rows_affected == 42: tran.commit() Yep. T

Re: Default scope of variables

2013-07-07 Thread Steven D'Aprano
On Fri, 05 Jul 2013 13:24:43 +, Neil Cerutti wrote: > for x in range(4): >print(x) > print(x) # Vader NOoOO!!! That loops do *not* introduce a new scope is a feature, not a bug. It is *really* useful to be able to use the value of x after the loop has finished. That's a much mor

Re: Default scope of variables

2013-07-07 Thread Steven D'Aprano
On Sun, 07 Jul 2013 23:43:24 +1000, Chris Angelico wrote: > On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner > wrote: >> Which you would then use like: >> >> >> conn = create_conn() >> with new_transaction(conn) as tran: >> rows_affected = do_query_stuff(tran) >> if rows_affected == 42: >>

Re: Default scope of variables

2013-07-07 Thread Chris Angelico
On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner wrote: > Which you would then use like: > > > conn = create_conn() > with new_transaction(conn) as tran: > rows_affected = do_query_stuff(tran) > if rows_affected == 42: > tran.commit() Yep. There's a problem, though, when you brin

Re: Default scope of variables

2013-07-07 Thread Wayne Werner
On Fri, 5 Jul 2013, Chris Angelico wrote: Oh. Uhm... ahh... it would have helped to mention that it also has a commit() method! But yes, that's correct; if the object expires (this is C++, so it's guaranteed to call the destructor at that close brace - none of the Python vagueness about when __d

Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-05, Chris Angelico wrote: > On Fri, Jul 5, 2013 at 11:24 PM, Neil Cerutti > wrote: >> Python provides deterministic destruction with a different >> feature. > > You mean 'with'? That's not actually destruction, it just does > one of the same jobs that deterministic destruction is used

Re: Default scope of variables

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 11:24 PM, Neil Cerutti wrote: > Python provides deterministic destruction with a different > feature. You mean 'with'? That's not actually destruction, it just does one of the same jobs that deterministic destruction is used for (RAII). It doesn't, for instance, have any in

Re: Default scope of variables

2013-07-05 Thread Neil Cerutti
On 2013-07-04, Dave Angel wrote: > On 07/04/2013 01:32 AM, Steven D'Aprano wrote: > >> Well, if I ever have more than 63,000,000 variables[1] in a >> function, I'll keep that in mind. >> > >> >> [1] Based on empirical evidence that Python supports names >> with length at least up to

Re: Default scope of variables

2013-07-04 Thread Rotwang
On 05/07/2013 02:24, Steven D'Aprano wrote: On Thu, 04 Jul 2013 17:54:20 +0100, Rotwang wrote: [...] Anyway, none of the calculations that has been given takes into account the fact that names can be /less/ than one million characters long. Not in *my* code they don't!!! *wink* The actual

Re: Default scope of variables

2013-07-04 Thread Joshua Landau
On 5 July 2013 03:03, Dave Angel wrote: > In particular, > http://docs.python.org/3.3/reference/lexical_analysis.html#identifiers > > has a definition for id_continue that includes several interesting > categories. I expected the non-ASCII digits, but there's other stuff there, > like "nonsp

Re: Default scope of variables

2013-07-04 Thread Joshua Landau
On 5 July 2013 03:03, Dave Angel wrote: > On 07/04/2013 09:24 PM, Steven D'Aprano wrote: >> On Thu, 04 Jul 2013 17:54:20 +0100, Rotwang wrote: >>> It's perhaps worth mentioning that some non-ascii characters are allowed >>> in identifiers in Python 3, though I don't know which ones. >> >> PEP 3131

Re: Default scope of variables

2013-07-04 Thread Dave Angel
On 07/04/2013 09:24 PM, Steven D'Aprano wrote: On Thu, 04 Jul 2013 17:54:20 +0100, Rotwang wrote: [...] Anyway, none of the calculations that has been given takes into account the fact that names can be /less/ than one million characters long. Not in *my* code they don't!!! *wink* The act

Re: Default scope of variables

2013-07-04 Thread Steven D'Aprano
On Thu, 04 Jul 2013 17:54:20 +0100, Rotwang wrote: [...] > Anyway, none of the calculations that has been given takes into account > the fact that names can be /less/ than one million characters long. Not in *my* code they don't!!! *wink* > The > actual number of non-empty strings of length a

Re: Default scope of variables

2013-07-04 Thread Joshua Landau
On 4 July 2013 17:54, Rotwang wrote: > 53*(63**100 - 1)//62 Or about 10**10**6.255 (so about 1.80M digits long). For the unicode side (Python 3, in other words) and reusing your math (ya better hope it's right!), you are talking: 97812*((97812+2020)**100 - 1)/(97812+2020-1) Or about 1

Re: Default scope of variables

2013-07-04 Thread Peter Otten
Rotwang wrote: > Sorry to be OT, but this is sending my pedantry glands haywire: We are mostly pedants, too -- so this is well-deserved... > On 04/07/2013 08:06, Dave Angel wrote: >> On 07/04/2013 01:32 AM, Steven D'Aprano wrote: >>> >> >>> >>> Well, if I ever have more than 63,000,000

Re: Default scope of variables

2013-07-04 Thread Chris Angelico
On Fri, Jul 5, 2013 at 2:38 AM, Steven D'Aprano wrote: > On Thu, 04 Jul 2013 15:47:57 +1000, Chris Angelico wrote: >> Here's one example of shadowing that comes from a C++ project at work. I >> have a class that represents a database transaction (constructing it >> begins a transaction, it has met

Re: Default scope of variables

2013-07-04 Thread Rotwang
Sorry to be OT, but this is sending my pedantry glands haywire: On 04/07/2013 08:06, Dave Angel wrote: On 07/04/2013 01:32 AM, Steven D'Aprano wrote: Well, if I ever have more than 63,000,000 variables[1] in a function, I'll keep that in mind. [1] Based on empirical eviden

Re: Default scope of variables

2013-07-04 Thread Steven D'Aprano
On Thu, 04 Jul 2013 15:47:57 +1000, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 3:32 PM, Steven D'Aprano > wrote: >> Accidental shadowing can be a problem, but I've never heard of anyone >> saying that they were *forced* to shadow a global they needed access >> to. Just pick a different name.

Re: Default scope of variables

2013-07-04 Thread Steven D'Aprano
On Thu, 04 Jul 2013 03:06:25 -0400, Dave Angel wrote: > On 07/04/2013 01:32 AM, Steven D'Aprano wrote: >> > >> >> Well, if I ever have more than 63,000,000 variables[1] in a function, >> I'll keep that in mind. >> > >> >> [1] Based on empirical evidence that Python supports names wit

Re: Default scope of variables

2013-07-04 Thread Wayne Werner
On Thu, 4 Jul 2013, Steven D'Aprano wrote: [1] Based on empirical evidence that Python supports names with length at least up to one million characters long, and assuming that each character can be an ASCII letter, digit or underscore. The specification *does* state unlimited length: http://

Re: Default scope of variables

2013-07-04 Thread Lele Gaifax
Dave Angel writes: > Well, the number wouldn't be 63,000,000. Rather it'd be 63**100 Uhm, if we are talking about Py2, then you should not count all the combinations starting with a digit, while under Py3 the number explodes, as this is valid code: >>> à = 1 >>> à 1 :-) back to easily-en

Re: Default scope of variables

2013-07-04 Thread Dave Angel
On 07/04/2013 01:32 AM, Steven D'Aprano wrote: Well, if I ever have more than 63,000,000 variables[1] in a function, I'll keep that in mind. [1] Based on empirical evidence that Python supports names with length at least up to one million characters long, and assuming that eac

Re: Default scope of variables

2013-07-04 Thread Ian Kelly
On Wed, Jul 3, 2013 at 11:32 PM, Steven D'Aprano wrote: >> Python lets you do that across but not within functions. >> >> But Javascript/ECMAScript/whatever doesn't give you that. A var >> declaration makes it function-local, no matter where the declaration is. >> That's pointless. C++, on the oth

Re: Default scope of variables

2013-07-03 Thread Peter Otten
Steven D'Aprano wrote: > Well, if I ever have more than 63,000,000 variables[1] in a function, > I'll keep that in mind. Until then, I'm pretty sure you can trivially > avoid name clashes with globals that you wish to avoid clashing with. > [1] Based on empirical evidence that Python supports nam

Re: Default scope of variables

2013-07-03 Thread Steven D'Aprano
On Thu, 04 Jul 2013 05:30:03 +0100, Joshua Landau wrote: > That said, I'm not too convinced. Personally, the proper way to do what > you are talking about is creating a new closure. Like: > > for i in range(100): > with new_scope(): > for i in range(100): > func(i) > f

Re: Default scope of variables

2013-07-03 Thread Chris Angelico
On Thu, Jul 4, 2013 at 3:32 PM, Steven D'Aprano wrote: > Accidental shadowing can be a problem, but I've never heard of anyone > saying that they were *forced* to shadow a global they needed access to. > Just pick a different name. Here's one example of shadowing that comes from a C++ project at

Re: Default scope of variables

2013-07-03 Thread Steven D'Aprano
On Thu, 04 Jul 2013 14:07:55 +1000, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 1:27 PM, Steven D'Aprano > wrote: >> With respect to the Huffman coding of declarations, Javascript gets it >> backwards. Locals ought to be more common, but they require more >> typing. Locals are safer, better, m

Re: Default scope of variables

2013-07-03 Thread Joshua Landau
On 4 July 2013 05:36, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau > wrote: >> That said, I'm not too convinced. Personally, the proper way to do >> what you are talking about is creating a new closure. Like: >> >> for i in range(100): >> with new_scope(): >>

Re: Default scope of variables

2013-07-03 Thread Chris Angelico
On Thu, Jul 4, 2013 at 2:30 PM, Joshua Landau wrote: > That said, I'm not too convinced. Personally, the proper way to do > what you are talking about is creating a new closure. Like: > > for i in range(100): > with new_scope(): > for i in range(100): > func(i) > func(i

Re: Default scope of variables

2013-07-03 Thread Joshua Landau
On 4 July 2013 05:07, Chris Angelico wrote: > On Thu, Jul 4, 2013 at 1:27 PM, Steven D'Aprano > wrote: >> With respect to the Huffman coding of declarations, Javascript gets it >> backwards. Locals ought to be more common, but they require more typing. >> Locals are safer, better, more desirable

Re: Default scope of variables

2013-07-03 Thread Chris Angelico
On Thu, Jul 4, 2013 at 1:27 PM, Steven D'Aprano wrote: > With respect to the Huffman coding of declarations, Javascript gets it > backwards. Locals ought to be more common, but they require more typing. > Locals are safer, better, more desirable than globals, and so it should > be easier to use lo

Default scope of variables

2013-07-03 Thread Steven D'Aprano
Recently, there was a thread where people discussed variable declarations, with a couple people stating that they wished that Python required you to declare local variables, instead of globals. I'm sure they have their (foolish, pathetic) *wink* reasons for this, but I thought I'd explain why I

Re: scope of variables

2006-05-04 Thread Gary Wessle
thank you -- http://mail.python.org/mailman/listinfo/python-list

Re: scope of variables

2006-05-04 Thread bruno at modulix
Gary Wessle wrote: > Ryan Forsythe <[EMAIL PROTECTED]> writes: > > >>Gary Wessle wrote: >> >>>the example was an in-accuretlly representation of a the problem I am >>>having. my apologies. >>> (snip) > I finally was able to duplicate the error with a through away code > as follows, > >

Re: scope of variables

2006-05-04 Thread Gary Wessle
Ryan Forsythe <[EMAIL PROTECTED]> writes: > Gary Wessle wrote: > > the example was an in-accuretlly representation of a the problem I am > > having. my apologies. > > > > a = [] > > def prnt(): > >print len(a) > > > prnt > > > > > > I expect to get 0 "the length of list a" > > You wa

Re: scope of variables

2006-05-03 Thread Rob E
> is the code below correct? > > b = 3 > def adding(a) > print a + b > > it seams not to see the up-level scope where b is defined. Yes except for the missing : at the end of the "def" line. Rob -- http://mail.python.org/mailman/listinfo/python-list

Re: scope of variables

2006-05-03 Thread Ryan Forsythe
Gary Wessle wrote: > the example was an in-accuretlly representation of a the problem I am > having. my apologies. > > a = [] > def prnt(): >print len(a) > prnt > > > I expect to get 0 "the length of list a" You want prnt(), not prnt: >>> a = [] >>> def prnt(): ... print len(a) ...

Re: scope of variables

2006-05-03 Thread Leif K-Brooks
Gary Wessle wrote: > the example was an in-accuretlly representation of a the problem I am > having. my apologies. > > a = [] > def prnt(): >print len(a) > prnt > > > I expect to get 0 "the length of list a" Python requires parenthesis to call a function. >>> a = [] >>> def prnt(

Re: scope of variables

2006-05-03 Thread Lord Landon
Try >>> prnt() o.o' On 04 May 2006 08:25:01 +1000, Gary Wessle <[EMAIL PROTECTED]> wrote: > "Steve R. Hastings" <[EMAIL PROTECTED]> writes: > > > On Thu, 04 May 2006 07:02:43 +1000, Gary Wessle wrote: > > > b = 3 > > > def adding(a) > > > print a + b > > > > > > it seams not to see the up-leve

Re: scope of variables

2006-05-03 Thread Gary Wessle
"Steve R. Hastings" <[EMAIL PROTECTED]> writes: > On Thu, 04 May 2006 07:02:43 +1000, Gary Wessle wrote: > > b = 3 > > def adding(a) > > print a + b > > > > it seams not to see the up-level scope where b is defined. > > Assuming you put a ':' after the "def adding(a)", this should work in >

Re: scope of variables

2006-05-03 Thread Ben Finney
Gary Wessle <[EMAIL PROTECTED]> writes: > is the code below correct? It's best to post an example that you've tried yourself, and that is small but completely demonstrates the issue in question. > b = 3 > def adding(a) > print a + b This, for example, would fail the syntax check (the 'def'

Re: scope of variables

2006-05-03 Thread Steve R. Hastings
On Thu, 04 May 2006 07:02:43 +1000, Gary Wessle wrote: > b = 3 > def adding(a) > print a + b > > it seams not to see the up-level scope where b is defined. Assuming you put a ':' after the "def adding(a)", this should work in recent versions of Python. In Python 2.0 and older, this will not

Re: scope of variables

2006-05-03 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Gary Wessle wrote: > is the code below correct? No... > b = 3 > def adding(a) ...a colon is missing at the end of the above line. > print a + b > > it seams not to see the up-level scope where b is defined. It does. And you could easily find out yourself by just

scope of variables

2006-05-03 Thread Gary Wessle
Hi is the code below correct? b = 3 def adding(a) print a + b it seams not to see the up-level scope where b is defined. thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: lists: += vs. .append() & oddness with scope of variables

2006-03-05 Thread Terry Reedy
"Sandro Dentella" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'd like to understand why += operator raises an error while .append() > does > not. Your mistake is thinking of '+=' as an operator. In Python terms it is not, any more than '=' is. In Python, neither 'a=b' nor

Re: lists: += vs. .append() & oddness with scope of variables

2006-03-05 Thread Scott David Daniels
Duncan Booth wrote: > Sandro Dentella wrote: > >> I'd like to understand why += operator raises an error while .append() >> does not. My wild guess is the parses treats them differently but I >> cannot understand why this depends on scope of the variables (global >> or class variables): > > >

Re: lists: += vs. .append() & oddness with scope of variables

2006-03-05 Thread Duncan Booth
Sandro Dentella wrote: > I'd like to understand why += operator raises an error while .append() > does not. My wild guess is the parses treats them differently but I > cannot understand why this depends on scope of the variables (global > or class variables): > > > a = [0] > > class foo(object)

Re: lists: += vs. .append() & oddness with scope of variables

2006-03-05 Thread Felipe Almeida Lessa
Em Dom, 2006-03-05 às 11:49 +, Sandro Dentella escreveu: > class foo(object): > > def __init__(self): > print "a: ", a > # += does not work if 'a' is global > #a += [1] > a.append(2) > print "a= ", a Try with: a = [0] class foo(object): def

lists: += vs. .append() & oddness with scope of variables

2006-03-05 Thread Sandro Dentella
I'd like to understand why += operator raises an error while .append() does not. My wild guess is the parses treats them differently but I cannot understand why this depends on scope of the variables (global or class variables): a = [0] class foo(object): def __init__(self): print