Re: [Tutor] set locals

2013-12-19 Thread spir
On 12/18/2013 09:45 PM, Alan Gauld wrote: On 18/12/13 17:45, Mark Lawrence wrote: Can I be so bold as to ask how discussing metaclasses and __setattr__ on a tutor mailing list is going to help the newbie who's having problems with their "hello world" program? It won't, but the tutor list is a

Re: [Tutor] set locals

2013-12-18 Thread eryksun
On Wed, Dec 18, 2013 at 6:16 AM, spir wrote: > On 12/18/2013 12:07 PM, eryksun wrote: >> >> You need __setattr__ from the metaclass: >> >> >>> class C: pass >> ... >> >>> type(C).__setattr__(C, "baz", "BAZ") >> >>> C.baz >> 'BAZ' > > Oh, that makes sense: so, __setattr__ o

Re: [Tutor] set locals

2013-12-18 Thread Steven D'Aprano
On Wed, Dec 18, 2013 at 05:45:02PM +, Mark Lawrence wrote: > Can I be so bold as to ask how discussing metaclasses and __setattr__ on > a tutor mailing list is going to help the newbie who's having problems > with their "hello world" program? It's not just newbies who need tutoring. Sometim

Re: [Tutor] set locals

2013-12-18 Thread Alan Gauld
On 18/12/13 17:45, Mark Lawrence wrote: Can I be so bold as to ask how discussing metaclasses and __setattr__ on a tutor mailing list is going to help the newbie who's having problems with their "hello world" program? It won't, but the tutor list is also for experienced programmers new to Pyth

Re: [Tutor] set locals

2013-12-18 Thread Mark Lawrence
On 18/12/2013 11:16, spir wrote: On 12/18/2013 12:07 PM, eryksun wrote: On Wed, Dec 18, 2013 at 5:40 AM, spir wrote: C.__setattr__(C, "baz", "BAZ") which fails, for any reason, with TypeError: can't apply this __setattr__ to type object You need __setattr__ from the metaclass:

Re: [Tutor] set locals

2013-12-18 Thread spir
On 12/18/2013 11:51 AM, eryksun wrote: On Tue, Dec 17, 2013 at 10:52 AM, spir wrote: is it at all possible to set new vars (or any symbol) into an existing scope (typically locals())? scope[name] = value raises by me an error like: TypeError: 'mappingproxy' object does not support it

Re: [Tutor] set locals

2013-12-18 Thread spir
On 12/18/2013 12:07 PM, eryksun wrote: On Wed, Dec 18, 2013 at 5:40 AM, spir wrote: C.__setattr__(C, "baz", "BAZ") which fails, for any reason, with TypeError: can't apply this __setattr__ to type object You need __setattr__ from the metaclass: >>> class C: pass ...

Re: [Tutor] set locals

2013-12-18 Thread eryksun
On Wed, Dec 18, 2013 at 5:40 AM, spir wrote: > C.__setattr__(C, "baz", "BAZ") > which fails, for any reason, with > TypeError: can't apply this __setattr__ to type object You need __setattr__ from the metaclass: >>> class C: pass ... >>> type(C).__setattr__(C, "baz", "BAZ")

Re: [Tutor] set locals

2013-12-18 Thread eryksun
On Tue, Dec 17, 2013 at 10:52 AM, spir wrote: > is it at all possible to set new vars (or any symbol) into an existing scope > (typically locals())? > > scope[name] = value > raises by me an error like: > TypeError: 'mappingproxy' object does not support item assignment > > I guess 'mappin

Re: [Tutor] set locals

2013-12-18 Thread spir
On 12/18/2013 10:02 AM, Peter Otten wrote: spir wrote: [...] Like Steven I have no idea how you produced the mappingproxy. Are you trying to use a class as a namespace (in Python 3.3)? class A: pass ... A.__dict__["foo"] = "bar" Traceback (most recent call last): File "", line 1, in Type

Re: [Tutor] set locals

2013-12-18 Thread Peter Otten
spir wrote: > Hello, > > is it at all possible to set new vars (or any symbol) into an existing > scope (typically locals())? locals() normally contains a copy of the current namespace as a dict. Setting items is possible but only alters the dict and has no effect on the original namespace: >

Re: [Tutor] set locals

2013-12-17 Thread Steven D'Aprano
On Tue, Dec 17, 2013 at 04:52:25PM +0100, spir wrote: > Hello, > > is it at all possible to set new vars (or any symbol) into an existing > scope (typically locals())? In general, no. The only time that writing to locals() is guaranteed to work is when you are in the top-level scope and locals

[Tutor] set locals

2013-12-17 Thread spir
Hello, is it at all possible to set new vars (or any symbol) into an existing scope (typically locals())? scope[name] = value raises by me an error like: TypeError: 'mappingproxy' object does not support item assignment I guess 'mappingproxy' is the implementation name of a scope (her