From: Mark Lawrence on 25 Jun 2010 17:11 On 25/06/2010 19:23, WANG Cong wrote: > On 06/25/10 14:31, Richard Thomas<chardster(a)gmail.com> wrote: > > <snip> > >> >> 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 needs. Performing additional >> checks every time an attribute assignment is performed is completely >> unnecessary. Remember that unlike C these checks would have to be >> performed at run-time. >> > > I don't care in which way I can limit this, I care why I should limit > this 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", the next it's a performance issue. What do you want, blood on it? Kindest regards. Mark Lawrence.
From: Neil Hodgson on 25 Jun 2010 19:11 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 Delphi did not implement this for efficiency reasons. Neil
From: Carl Banks on 25 Jun 2010 22:31 On Jun 25, 6:15 am, WANG Cong <xiyou.wangc...(a)gmail.com> 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 = "hello" > >>> test.a > 'hello' > > However, I still don't get the points why Python designs it like this. [snip points] I understand you and think your proposal is reasonable. I know a few people who use Python who would agree with you, and even go farther. You did well to propose not banning attributes per se, but making it harder to set them. Python isn't really in the business of making questionable things impossible (there are occasions where it does, like killing threads), but it is in the business of making things that are questionable enough harder. What you propose is reasonable under Python's design principles (provided that initializer functions were exempted), and if Python had been designed as you propose, I don't think it would have suffered that much. 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. I know you won't agree with this, and I'm not trying to convince you or argue with you. I'm just telling you why it's like that. 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. Carl Banks
From: Steven D'Aprano on 25 Jun 2010 22:40 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
From: WANG Cong on 25 Jun 2010 23:24
On 06/26/10 03:31, Carl Banks <pavlovevidence(a)gmail.com> wrote: > On Jun 25, 6:15 am, WANG Cong <xiyou.wangc...(a)gmail.com> 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 = "hello" >> >>> test.a >> 'hello' >> >> However, I still don't get the points why Python designs it like this. > [snip points] > > > I understand you and think your proposal is reasonable. I know a few > people who use Python who would agree with you, and even go farther. > > You did well to propose not banning attributes per se, but making it > harder to set them. Python isn't really in the business of making > questionable things impossible (there are occasions where it does, > like killing threads), but it is in the business of making things that > are questionable enough harder. What you propose is reasonable under > Python's design principles (provided that initializer functions were > exempted), and if Python had been designed as you propose, I don't > think it would have suffered that much. > Thanks much for encouraging me! This makes me feeling less guilty. ;-) > 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 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 related with dynamic programming or not. > I know you won't agree with this, and I'm not trying to convince you > or argue with you. I'm just telling you why it's like that. > > 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. > In fact I wanted to avoid comparing Python with C++. :) When I talked about OOP, it is general OOP, not related with any concrete programming languages. -- Live like a child, think like the god. |