Inheriting from "dict" (was: Python dynamic attribute creation)

2010-07-08 Thread John Nagle
On 7/2/2010 1:54 AM, Gregory Ewing wrote: WANG Cong wrote: Yeah, my point is why setattr() for dynamic attributes while assignments for static attributes? If you mean using exactly the same syntax for both, that would require making the static case more verbose, e.g. instead of foo.blarg yo

Re: Python dynamic attribute creation

2010-07-02 Thread Bruno Desthuilliers
WANG Cong a écrit : On 06/30/10 01:25, Ethan Furman wrote: But if so why setattr() still exists? What is it for if we can do the same thing via assignments? Also, in order to be perfect, Python should accept to add dynamic attributes dynamically, something like PEP 363. That doesn't happen. S

Re: Python dynamic attribute creation

2010-07-02 Thread Bruno Desthuilliers
WANG Cong a écrit : On 07/01/10 23:19, Stephen Hansen wrote: As long as setattr() exists in Python, that will be not so ordinary. :) setattr is perfectly ordinary. If you think setattr() is as ordinary as a trivial assignment, setattr IS a trivial assignment. However, I think setattr(

Re: Python dynamic attribute creation

2010-07-02 Thread Gregory Ewing
WANG Cong wrote: However, I think setattr() is a builtin function, using it exposes the *magic* of metaprogramming (or class-programming, if more correct) at a first glance. But, in Python, creating instance variables is *not* class-programming. It doesn't touch the class at all. In many OO l

Re: Python dynamic attribute creation

2010-07-02 Thread Gregory Ewing
WANG Cong wrote: When I talked about OOP, it is general OOP, not related with > any concrete programming languages. There isn't really any such thing, though. There is no universally agreed set of features that a language must have in order to be considered OOP. Arguments of the form "Language

Re: Python dynamic attribute creation

2010-07-02 Thread Gregory Ewing
WANG Cong wrote: Yeah, my point is why setattr() for dynamic attributes while assignments for static attributes? I think there may be a misunderstanding here. You seem to be thinking of "dynamic attribute" vs. "static attribute" as the distinction between creating a new attribute and modifying

Re: Python dynamic attribute creation

2010-07-02 Thread Gregory Ewing
WANG Cong wrote: If you think setattr() is as ordinary as a trivial assignment, I will argue with you, this is personal taste. To my way of thinking, getattr() and setattr() are the fundamental way of accessing attributes in Python. The dot notation is just syntactic sugar for the overwhelming

Re: Python dynamic attribute creation

2010-07-01 Thread Chris Rebert
On Thu, Jul 1, 2010 at 6:10 PM, Steven D'Aprano wrote: > Engineers are quite > happy to make the tools they need to make the tools they need to make the > tools they need to make something. Carpenters would think you were crazy > if you said that building a scaffold was "meta-carpentry" and there

Re: Python dynamic attribute creation

2010-07-01 Thread Steven D'Aprano
On Thu, 01 Jul 2010 23:46:55 +0800, WANG Cong wrote: > However, I think setattr() is a builtin function, using it exposes the > *magic* of metaprogramming (or class-programming, if more correct) at a > first glance. There's nothing magic about metaprogramming. If you're a programmer, you write p

Re: Python dynamic attribute creation

2010-07-01 Thread Rami Chowdhury
On 2010-07-01 23:42, WANG Cong wrote: > On 07/01/10 22:53, Stephen Hansen wrote: > > > > > One uses assignment syntax when the name of the attribute they are > > setting is known at the time when one writes the code. > > > > One uses the setattr function when the name of the attribute is not > >

Re: Python dynamic attribute creation

2010-07-01 Thread Stephen Hansen
On 7/1/10 8:46 AM, WANG Cong wrote: However, I think setattr() is a builtin function, using it exposes the *magic* of metaprogramming (or class-programming, if more correct) at a first glance. I'm going to try this one more time -- you place a great deal of importance and special properties on

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 07/01/10 23:19, Stephen Hansen wrote: >> >> As long as setattr() exists in Python, that will be not so ordinary. :) > > setattr is perfectly ordinary. If you think setattr() is as ordinary as a trivial assignment, I will argue with you, this is personal taste. However, I think setattr() is a

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 07/01/10 22:53, Stephen Hansen wrote: > > One uses assignment syntax when the name of the attribute they are > setting is known at the time when one writes the code. > > One uses the setattr function when the name of the attribute is not > known until runtime. > > The difference has *nothing a

Re: Python dynamic attribute creation

2010-07-01 Thread Stephen Hansen
On 7/1/10 8:02 AM, WANG Cong wrote: On 06/27/10 09:06, Steven D'Aprano wrote: In that situation, certainly: adding an attribute on the fly to that formal definition seems entirely strange and special of an activity. But that's only because you *chose* to *see* and *use* the object that way. Th

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 06/28/10 17:43, Bruno Desthuilliers wrote: > Carl Banks a écrit : >> On Jun 27, 3:49 am, Bruno Desthuilliers >> wrote: >>> WANG Cong a écrit : >>> On 06/26/10 00:11, Neil Hodgson wrote: > WANG Cong: >> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, >> this

Re: Python dynamic attribute creation

2010-07-01 Thread Stephen Hansen
On 7/1/10 7:44 AM, WANG Cong wrote: On 07/01/10 13:49, Stephen Hansen wrote: It may not be "the" primary concern, but elegance certainly is *a* primary concern. I concur. Its not explicitly stated, but it is the Zen 0. This is further supported by its implied presence in many of the Axioms a

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 06/27/10 09:06, Steven D'Aprano wrote: >> In that situation, certainly: adding an attribute on the fly to that >> formal definition seems entirely strange and special of an activity. But >> that's only because you *chose* to *see* and *use* the object that way. >> The "special"ness of the acti

Re: Python dynamic attribute creation

2010-07-01 Thread Aahz
In article , WANG Cong wrote: >On 07/01/10 13:49, Stephen Hansen wrote: >>Wang Cong deleted the attribution for Aahz: >>> >>> It may not be "the" primary concern, but elegance certainly is *a* >>> primary concern. >> >> I concur. >> >> Its not explicitly stated, but it is the Zen 0. This is furt

Re: Python dynamic attribute creation

2010-07-01 Thread Aahz
In article , Michael Torrie wrote: >On 06/28/2010 02:31 PM, Aahz wrote: >> In article , >> Michael Torrie wrote: >>> >>> True. But you can't really criticize a language's implementation of OOP >>> without a good understanding of the "pure" OO language. For example, in >>> Smalltalk If/Then st

Re: Python dynamic attribute creation

2010-07-01 Thread Stephen Hansen
On 7/1/10 7:31 AM, WANG Cong wrote: On 06/30/10 01:20, Stephen Hansen wrote: But if so why setattr() still exists? What is it for if we can do the same thing via assignments? Also, in order to be perfect, Python should accept to add dynamic attributes dynamically, something like PEP 363. That

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 07/01/10 13:49, Stephen Hansen wrote: Hi, Stephen, >> >> It may not be "the" primary concern, but elegance certainly is *a* >> primary concern. > > I concur. > > Its not explicitly stated, but it is the Zen 0. This is further > supported by its implied presence in many of the Axioms and Truth

Re: Python dynamic attribute creation

2010-07-01 Thread Stephen Hansen
On 7/1/10 5:41 AM, Bruno Desthuilliers wrote: Stephen Hansen a écrit : On 6/30/10 10:37 PM, Aahz wrote: It may not be "the" primary concern, but elegance certainly is *a* primary concern. I concur. Its not explicitly stated, but it is the Zen 0. This is further supported by its implied prese

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 06/30/10 01:20, Stephen Hansen wrote: >> But if so why setattr() still exists? What is it for if we can do the >> same thing via assignments? Also, in order to be perfect, Python should >> accept to add dynamic attributes dynamically, something like PEP >> 363. That doesn't happen. > > What do

Re: Python dynamic attribute creation

2010-07-01 Thread WANG Cong
On 06/30/10 01:25, Ethan Furman wrote: >> But if so why setattr() still exists? What is it for if we can do the >> same thing via assignments? Also, in order to be perfect, Python should >> accept to add dynamic attributes dynamically, something like PEP >> 363. That doesn't happen. > > Setattr a

Re: Python dynamic attribute creation

2010-07-01 Thread Bruno Desthuilliers
Stephen Hansen a écrit : On 6/30/10 10:37 PM, Aahz wrote: In article<4c29ad38$0$26210$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article<4c285e7c$0$17371$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article<4c2747c1$0$4545$426a7...@news.fre

Re: Python dynamic attribute creation

2010-06-30 Thread Stephen Hansen
On 6/30/10 10:37 PM, Aahz wrote: In article<4c29ad38$0$26210$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article<4c285e7c$0$17371$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article<4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers

Re: Python dynamic attribute creation

2010-06-30 Thread Aahz
In article <4c29ad38$0$26210$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >Aahz a écrit : >> In article <4c285e7c$0$17371$426a7...@news.free.fr>, >> Bruno Desthuilliers wrote: >>> Aahz a écrit : In article <4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >>>

Re: Python dynamic attribute creation

2010-06-30 Thread Andre Alexander Bell
On 06/29/2010 06:46 PM, WANG Cong wrote: > On 06/29/10 17:48, Andre Alexander Bell wrote: > var a > a >> -> should raise an variable 'unset' exception >> >> Keep in mind that the module you are writing in is just an object as is >> any function or method. So using local variables therein y

Re: Python dynamic attribute creation

2010-06-29 Thread Carl Banks
On Jun 29, 9:48 am, WANG Cong wrote: > On 06/27/10 12:01, Carl Banks wrote: > > > > > > > On Jun 25, 8:24 pm, WANG Cong wrote: > >> Understand, but please consider my proposal again, if we switched to: > > >> setattr(foo, 'new_attr', "blah") > > >> by default, isn't Python still dynamic as it is

Re: Python dynamic attribute creation

2010-06-29 Thread Stephen Hansen
On 6/29/10 9:46 AM, WANG Cong wrote: 1) Disallow dynamic attribute creations by assignments _by default_, thus I expect an error when I do: So far I only did tell you _how_ it is in Python. If I understand your question about the design of the language correctly than you would like Python to de

Re: Python dynamic attribute creation

2010-06-29 Thread Stephen Hansen
On 6/29/10 9:48 AM, WANG Cong wrote: On 06/27/10 12:01, Carl Banks wrote: On Jun 25, 8:24 pm, WANG Cong wrote: Understand, but please consider my proposal again, if we switched to: setattr(foo, 'new_attr', "blah") by default, isn't Python still dynamic as it is? (Please teach me if I am wr

Re: Python dynamic attribute creation

2010-06-29 Thread Ethan Furman
WANG Cong wrote: On 06/29/10 17:48, Andre Alexander Bell wrote: As said previously I don't think one should differentiate between meta programming and programming within the language, since the former is nothing different than the latter. If you check other programming language rather than

Re: Python dynamic attribute creation

2010-06-29 Thread Ethan Furman
WANG Cong wrote: On 06/27/10 12:01, Carl Banks wrote: On Jun 25, 8:24 pm, WANG Cong wrote: Understand, but please consider my proposal again, if we switched to: setattr(foo, 'new_attr', "blah") by default, isn't Python still dynamic as it is? (Please teach me if I am wrong here.) This why

Re: Python dynamic attribute creation

2010-06-29 Thread Chris Rebert
On Tue, Jun 29, 2010 at 9:48 AM, WANG Cong wrote: > On 06/27/10 12:01, Carl Banks wrote: >> On Jun 25, 8:24 pm, WANG Cong wrote: >>> Understand, but please consider my proposal again, if we switched to: >>> >>> setattr(foo, 'new_attr', "blah") >>> >>> by default, isn't Python still dynamic as it

Re: Python dynamic attribute creation

2010-06-29 Thread WANG Cong
On 06/27/10 12:01, Carl Banks wrote: > On Jun 25, 8:24 pm, WANG Cong wrote: >> Understand, but please consider my proposal again, if we switched to: >> >> setattr(foo, 'new_attr', "blah") >> >> by default, isn't Python still dynamic as it is? (Please teach me if I >> am wrong here.) >> >> This w

Re: Python dynamic attribute creation

2010-06-29 Thread WANG Cong
On 06/29/10 17:48, Andre Alexander Bell wrote: > On 06/25/2010 03:15 PM, WANG Cong wrote: >> 1) Modifying a class attribute is metaprogramming, and this is modifying >> a class, i.e. adding a new attribute to it, thus this should belong >> to metaprogramming. (I know, strictly speaking, maybe my

Re: Python dynamic attribute creation

2010-06-29 Thread Andre Alexander Bell
On 06/25/2010 03:15 PM, WANG Cong wrote: > 1) Modifying a class attribute is metaprogramming, and this is modifying > a class, i.e. adding a new attribute to it, thus this should belong > to metaprogramming. (I know, strictly speaking, maybe my definition of > "metaprogramming" here is incorrect, I

Re: Python dynamic attribute creation

2010-06-29 Thread Bruno Desthuilliers
Aahz a écrit : In article <4c285e7c$0$17371$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article <4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Python has no pretention at "elegance". That's not true at all. More precisely, I would agree with

Re: [OT] Football was: Python dynamic attribute creation

2010-06-28 Thread Mark Lawrence
On 29/06/2010 00:21, Alexander Kapps wrote: Mark Lawrence wrote: On 28/06/2010 20:23, Alexander Kapps wrote: UHHM! Forget it. This of course doesn't work with setattr too. My stupidness. :-( Don't worry too much, looks like your nation's football is much better than your settattr knowledge.

[OT] Football was: Python dynamic attribute creation

2010-06-28 Thread Alexander Kapps
Mark Lawrence wrote: On 28/06/2010 20:23, Alexander Kapps wrote: UHHM! Forget it. This of course doesn't work with setattr too. My stupidness. :-( Don't worry too much, looks like your nation's football is much better than your settattr knowledge. I very much appreciate your attempt to make

Re: Python dynamic attribute creation

2010-06-28 Thread Mark Lawrence
On 28/06/2010 20:23, Alexander Kapps wrote: Alexander Kapps wrote: Bruno Desthuilliers wrote: Alexander Kapps a écrit : (snip) While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, th

Re: Python dynamic attribute creation

2010-06-28 Thread Michael Torrie
On 06/28/2010 02:31 PM, Aahz wrote: > In article , > Michael Torrie wrote: >> True. But you can't really criticize a language's implementation of OOP >> without a good understanding of the "pure" OO language. For example, in >> Smalltalk If/Then statements are actually methods of Boolean object

Re: Python dynamic attribute creation

2010-06-28 Thread Aahz
In article , Michael Torrie wrote: >On 06/27/2010 11:58 PM, Stephen Hansen wrote: >> >> To say you can't really know "much about" OOP without knowing "much >> about" Smalltalk seems basically, well, wrong. > >True. But you can't really criticize a language's implementation of OOP >without a goo

Re: Python dynamic attribute creation

2010-06-28 Thread Michael Torrie
On 06/27/2010 11:58 PM, Stephen Hansen wrote: > To say you can't really know "much about" OOP without knowing "much > about" Smalltalk seems basically, well, wrong. True. But you can't really criticize a language's implementation of OOP without a good understanding of the "pure" OO language. Fo

Re: Python dynamic attribute creation

2010-06-28 Thread Michael Torrie
On 06/25/2010 09:39 PM, WANG Cong wrote: > Thanks, I have to admit that I know nothing about Smalltalk. If you know nothing about Smalltalk then you really aren't in a position to talk about what is and is not OOP. Smalltalk is one of the original OOP languages and purists define OOP as the model

Re: Python dynamic attribute creation

2010-06-28 Thread Stephen Hansen
On 6/28/10 12:09 PM, Alexander Kapps wrote: This seems to work quite well: class TypoProtection(object): def __init__(self): self.foo = 42 self.bar = 24 def _setattr(self, name, value): if name in self.__dict__: self.__dict__[name] = value else: raise AttributeError, "%s has no '%s' attribute"

Re: Python dynamic attribute creation

2010-06-28 Thread Alexander Kapps
Alexander Kapps wrote: Bruno Desthuilliers wrote: Alexander Kapps a écrit : (snip) While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, those typos that seem to catch newcommers won

Re: Python dynamic attribute creation

2010-06-28 Thread Alexander Kapps
Bruno Desthuilliers wrote: Alexander Kapps a écrit : (snip) While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, those typos that seem to catch newcommers won't happen anymore. What

Re: Python dynamic attribute creation

2010-06-28 Thread Stephen Hansen
On 6/28/10 8:27 AM, Aahz wrote: In article<4c285e7c$0$17371$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Aahz a écrit : In article<4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Python has no pretention at "elegance". That's not true at all. More precisely, I wo

Re: Python dynamic attribute creation

2010-06-28 Thread Aahz
In article <4c285e7c$0$17371$426a7...@news.free.fr>, Bruno Desthuilliers wrote: >Aahz a écrit : >> In article <4c2747c1$0$4545$426a7...@news.free.fr>, >> Bruno Desthuilliers wrote: >>> >>> Python has no pretention at "elegance". >> >> That's not true at all. More precisely, I would agree wit

Re: Python dynamic attribute creation

2010-06-28 Thread Bruno Desthuilliers
Alexander Kapps a écrit : (snip) While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, those typos that seem to catch newcommers won't happen anymore. What I mean is this: class Foo(

Re: Python dynamic attribute creation

2010-06-28 Thread Bruno Desthuilliers
Carl Banks a écrit : On Jun 27, 3:49 am, Bruno Desthuilliers wrote: WANG Cong a écrit : On 06/26/10 00:11, Neil Hodgson wrote: WANG Cong: 4) Also, this will _somewhat_ violate the OOP princples, in OOP, this is and should be implemented by inherence. Most object oriented programming la

Re: Python dynamic attribute creation

2010-06-28 Thread Bruno Desthuilliers
Aahz a écrit : In article <4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers wrote: Python has no pretention at "elegance". That's not true at all. More precisely, I would agree with you if the emphasis is on "pretention" but not if the emphasis is on elegance; Python Zen, #9 (o

Re: Python dynamic attribute creation

2010-06-28 Thread Aahz
In article , Neil Hodgson wrote: >WANG Cong: >> >> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, >> this is and should be implemented by inherence. > > Most object oriented programming languages starting with Smalltalk >have allowed adding attributes (addInstVarName) to class

Re: Python dynamic attribute creation

2010-06-28 Thread Aahz
In article <4c2747c1$0$4545$426a7...@news.free.fr>, Bruno Desthuilliers wrote: > >Python has no pretention at "elegance". That's not true at all. More precisely, I would agree with you if the emphasis is on "pretention" but not if the emphasis is on elegance; I think that Python is extremely e

Re: Python dynamic attribute creation

2010-06-27 Thread geremy condra
On Sun, Jun 27, 2010 at 6:49 AM, Bruno Desthuilliers wrote: > WANG Cong a écrit : >> On 06/26/10 00:11, Neil Hodgson wrote: >> >>> WANG Cong: >>> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, this is and should be implemented by inherence. >>>    Most object oriented

Re: Python dynamic attribute creation

2010-06-27 Thread Stephen Hansen
On 6/27/10 10:10 PM, Carl Banks wrote: On Jun 27, 3:49 am, Bruno Desthuilliers wrote: WANG Cong a écrit : On 06/26/10 00:11, Neil Hodgson wrote: WANG Cong: 4) Also, this will _somewhat_ violate the OOP princples, in OOP, this is and should be implemented by inherence. Most object

Re: Python dynamic attribute creation

2010-06-27 Thread Carl Banks
On Jun 27, 3:49 am, Bruno Desthuilliers wrote: > WANG Cong a écrit : > > > On 06/26/10 00:11, Neil Hodgson wrote: > > >> WANG Cong: > > >>> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, > >>> this is and should be implemented by inherence. > >>    Most object oriented programmi

Re: Python dynamic attribute creation

2010-06-27 Thread Bruno Desthuilliers
WANG Cong a écrit : > On 06/26/10 00:11, Neil Hodgson wrote: > >> WANG Cong: >> >>> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, >>> this is and should be implemented by inherence. >>Most object oriented programming languages starting with Smalltalk >> have allowed adding

Re: Python dynamic attribute creation

2010-06-27 Thread Bruno Desthuilliers
WANG Cong a écrit : > On 06/25/10 17:25, Steven D'Aprano > wrote: > >> On Fri, 25 Jun 2010 14:15:12 +0100, WANG Cong wrote: >> (snip) >>> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, this is >>> and should be implemented by inherence. >> Perhaps, and perhaps not. But Python h

Re: Python dynamic attribute creation

2010-06-27 Thread Bruno Desthuilliers
WANG Cong a écrit : (snip) > > The point is why making metaprogramming easy is wonderful? Because it makes life easier ?-) > AND, even if > it were wonderful, why only this one, i.e. creating attributes by > assignments, not other things? Like : class Test(object): a = 1 del Test.a ?-)

Re: Python dynamic attribute creation

2010-06-27 Thread Bruno Desthuilliers
WANG Cong a écrit : > On 06/25/10 15:34, Bruno Desthuilliers > wrote: > >> WANG Cong a écrit : >>> Hi, list! >>> >>> I have a doubt about the design of dynamic attribute creation by >>> assignments in Python. >>> >>> As we know, in Python, we are able to create a new attribute of >>> a class dyn

Re: Python dynamic attribute creation

2010-06-26 Thread Carl Banks
On Jun 25, 8:24 pm, WANG Cong wrote: > Understand, but please consider my proposal again, if we switched to: > > setattr(foo, 'new_attr', "blah") > > by default, isn't Python still dynamic as it is? (Please teach me if I > am wrong here.) > > This why I said the questionable thing is not so much r

Re: Python dynamic attribute creation

2010-06-26 Thread Steven D'Aprano
On Sat, 26 Jun 2010 18:35:25 -0700, Chris Rebert wrote: > On Sat, Jun 26, 2010 at 6:06 PM, Steven D'Aprano > wrote: >> CPython is a fairly plodding implementation. But that's due to the >> conservativeness of CPython: Unladen Swallow is faster, and PyPI is >> faster still, and the PyPI people ex

Re: Python dynamic attribute creation

2010-06-26 Thread Chris Rebert
On Sat, Jun 26, 2010 at 6:06 PM, Steven D'Aprano wrote: > CPython is a fairly plodding implementation. But that's due to the > conservativeness of CPython: Unladen Swallow is faster, and PyPI is > faster still, and the PyPI people expect to eventually be competitive > with C for speed. PyPy (htt

Re: Python dynamic attribute creation

2010-06-26 Thread Steven D'Aprano
On Sat, 26 Jun 2010 10:10:39 -0700, Stephen Hansen wrote: > This characterization of adding attributes to an object as something > "else", some special kind of activity called "metaprogramming" I think I > reject outright, whereas I believe -- though I do not claim to speak for > him/her -- the OP

Re: Python dynamic attribute creation

2010-06-26 Thread Alexander Kapps
Stephen Hansen wrote: On 6/26/10 9:01 AM, Alexander Kapps wrote: While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, those typos that seem to catch newcommers won't happen anymore. Wh

Re: Python dynamic attribute creation

2010-06-26 Thread Stephen Hansen
On 6/26/10 9:01 AM, Alexander Kapps wrote: While I personally don't agree with this proposal (but I understand why some people might want it), I can see a reason. When disallowing direct attribute creation, those typos that seem to catch newcommers won't happen anymore. What I mean is this: I

Re: Python dynamic attribute creation

2010-06-26 Thread Alexander Kapps
Ixokai wrote: In what possible way is: setattr(foo, 'new_attr', 'blah') getattr(foo, 'new_attr') delattr(foo, 'new_attr') Better then: foo.new_attr = 'blah' foo.new_attr del foo.new_attr I don't understand what your argument is or problem is with the regular syntax,

Re: Python dynamic attribute creation

2010-06-26 Thread Thomas Jollans
On 06/26/2010 05:39 AM, WANG Cong wrote: > On 06/26/10 00:11, Neil Hodgson wrote: > >> WANG Cong: >> >>> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, >>> this is and should be implemented by inherence. >> >>Most object oriented programming languages starting with Smalltalk

Re: Python dynamic attribute creation

2010-06-26 Thread Neil Hodgson
WANG Cong: > From what you are saying, Smalltalk picks a way similar to setattr() in > Python? addInstVarName is a method on ClassDescription objects. > Because you mentioned 'addInstVarName' which seems to be a > method or a builtin function. If so, that is my point, as I mentioned > earlier

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/25/10 20:22, Ian Kelly wrote: > On Fri, Jun 25, 2010 at 12:51 PM, Stephen Hansen > wrote: >>> Using assignments to create an attribute hides metaprogramming behide, >>> while using delattr() exposes it. >> >> I don't understand what you're saying here either. > > I think he's saying that w

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
be a performance issue, but comparing >> it with a language design issue, _I think_ the latter thing is much more >> important than the former one. >> > > Blimey, one minute we're talking about "Python dynamic attribute > creation", the next it's a pe

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/25/10 19:38, Ethan Furman wrote: > WANG Cong wrote: >> On 06/25/10 15:34, Bruno Desthuilliers >> wrote: >> >>> WANG Cong a écrit : Hi, list! I have a doubt about the design of dynamic attribute creation by assignments in Python. As we know, in Python, we are

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/26/10 00:11, Neil Hodgson wrote: > WANG Cong: > >> 4) Also, this will _somewhat_ violate the OOP princples, in OOP, >> this is and should be implemented by inherence. > >Most object oriented programming languages starting with Smalltalk > have allowed adding attributes (addInstVarName)

Re: Python dynamic attribute creation

2010-06-25 Thread Ixokai
On Fri, Jun 25, 2010 at 8:24 PM, WANG Cong wrote: > > Here's the thing: Python doesn't consider creating dynamic attributes > > to be questionable. Python doesn't merely allow for dynamicism, it > > encourages it. And encouraging something means to make it simple. > > > > Understand, but please

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/26/10 03:31, Carl Banks wrote: > On Jun 25, 6:15 am, WANG Cong wrote: >> Hi, list! >> >> I have a doubt about the design of dynamic attribute creation by >> assignments in Python. >> >> As we know, in Python, we are able to create a new attribute of >> a class dynamically by an assignment:

Re: Python dynamic attribute creation

2010-06-25 Thread Steven D'Aprano
On Fri, 25 Jun 2010 19:31:24 -0700, Carl Banks wrote: > I will also tell you that claims like "it's doesn't use good OOP > principles" or "C++ does it that way" carry almost no credit. How C++ > does something is a suggestion, nothing more. And sometimes a horrible warning *wink* -- Steven --

Re: Python dynamic attribute creation

2010-06-25 Thread Carl Banks
On Jun 25, 6:15 am, WANG Cong wrote: > Hi, list! > > I have a doubt about the design of dynamic attribute creation by > assignments in Python. > > As we know, in Python, we are able to create a new attribute of > a class dynamically by an assignment: > > > > >>> class test: pass > ... > >>> test.a

Re: Python dynamic attribute creation

2010-06-25 Thread Neil Hodgson
WANG Cong: > 4) Also, this will _somewhat_ violate the OOP princples, in OOP, > this is and should be implemented by inherence. Most object oriented programming languages starting with Smalltalk have allowed adding attributes (addInstVarName) to classes at runtime. Low level OOPLs like C++ and

Re: Python dynamic attribute creation

2010-06-25 Thread Mark Lawrence
is by default, not vice versa? Yeah, I do understand this could be a performance issue, but comparing it with a language design issue, _I think_ the latter thing is much more important than the former one. Blimey, one minute we're talking about "Python dynamic attribute creation&qu

Re: Python dynamic attribute creation

2010-06-25 Thread Mark Lawrence
On 25/06/2010 19:03, WANG Cong wrote: [lots of snips] "Happily mixes them all together" doesn't mean it is elegant. :) Bollocks springs to my mind. :) Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list

Re: Python dynamic attribute creation

2010-06-25 Thread Ian Kelly
On Fri, Jun 25, 2010 at 12:51 PM, Stephen Hansen wrote: >> Using assignments to create an attribute hides metaprogramming behide, >> while using delattr() exposes it. > > I don't understand what you're saying here either. I think he's saying that when an attribute exists in the class dictionary,

Re: Python dynamic attribute creation

2010-06-25 Thread Stephen Hansen
On Fri, Jun 25, 2010 at 11:03 AM, WANG Cong wrote: > On 06/25/10 17:25, Steven D'Aprano > wrote: > > > Yes, isn't it wonderful? In other languages, metaprogramming is deepest > > black magic, or even completely impossible. In Python it is so easy that > > anyone can do it, and it is something beg

Re: Python dynamic attribute creation

2010-06-25 Thread Ethan Furman
WANG Cong wrote: On 06/25/10 15:34, Bruno Desthuilliers wrote: WANG Cong a écrit : Hi, list! I have a doubt about the design of dynamic attribute creation by assignments in Python. As we know, in Python, we are able to create a new attribute of a class dynamically by an assignment: class

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/25/10 14:31, Richard Thomas wrote: > > If you desperately want to limit the attribute assignments that can be > performed on an object you can set the __slots__ attribute of its > type. However, the Python ethos has always been to restrict as little > as necessary to provide the tools it

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/25/10 15:34, Bruno Desthuilliers wrote: > WANG Cong a écrit : >> Hi, list! >> >> I have a doubt about the design of dynamic attribute creation by >> assignments in Python. >> >> As we know, in Python, we are able to create a new attribute of >> a class dynamically by an assignment: >>

Re: Python dynamic attribute creation

2010-06-25 Thread WANG Cong
On 06/25/10 17:25, Steven D'Aprano wrote: > On Fri, 25 Jun 2010 14:15:12 +0100, WANG Cong wrote: > >> Hi, list! >> >> I have a doubt about the design of dynamic attribute creation by >> assignments in Python. >> >> As we know, in Python, we are able to create a new attribute of a class >> dynam

Re: Python dynamic attribute creation

2010-06-25 Thread Stephen Hansen
On Fri, Jun 25, 2010 at 6:15 AM, WANG Cong wrote: > 4) Also, this will _somewhat_ violate the OOP princples, in OOP, > this is and should be implemented by inherence. > Others have answered the rest fine, I just wanted to add: Who says we have to follow OOP principles, huh? If you want to follo

Re: Python dynamic attribute creation

2010-06-25 Thread Steven D'Aprano
On Fri, 25 Jun 2010 14:15:12 +0100, WANG Cong wrote: > Hi, list! > > I have a doubt about the design of dynamic attribute creation by > assignments in Python. > > As we know, in Python, we are able to create a new attribute of a class > dynamically by an assignment: > class test: pass > ..

Re: Python dynamic attribute creation

2010-06-25 Thread Bruno Desthuilliers
WANG Cong a écrit : Hi, list! I have a doubt about the design of dynamic attribute creation by assignments in Python. As we know, in Python, we are able to create a new attribute of a class dynamically by an assignment: class test: pass ... test.a = "hello" test.a 'hello' However, I still

Re: Python dynamic attribute creation

2010-06-25 Thread Nathan Rice
You are thinking like a C programmer Why do you want the language to tie your hands? I want a language to give me the tools I need and get out of the way. The more assumptions that are baked into a language the more opportunities it has to be wrong. Furthermore, object oriented design is a

Re: Python dynamic attribute creation

2010-06-25 Thread Richard Thomas
On Jun 25, 2:15 pm, WANG Cong wrote: > Hi, list! > > I have a doubt about the design of dynamic attribute creation by > assignments in Python. > > As we know, in Python, we are able to create a new attribute of > a class dynamically by an assignment: > > > > >>> class test: pass > ... > >>> test.a

Python dynamic attribute creation

2010-06-25 Thread WANG Cong
Hi, list! I have a doubt about the design of dynamic attribute creation by assignments in Python. As we know, in Python, we are able to create a new attribute of a class dynamically by an assignment: >>> class test: pass ... >>> test.a = "hello" >>> test.a 'hello' >>> However, I still don't