Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51675

Re: Python descriptor protocol (for more or less structured data)

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Python descriptor protocol (for more or less structured data)
Date 2013-07-31 13:44 -0400
References <040f33f6-6435-4aea-98ae-eabf8c16b167@googlegroups.com> <mailman.5310.1375194933.3114.python-list@python.org> <7ba674c2-9645-4644-8254-4407905e3270@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.41.1375292670.1251.python-list@python.org> (permalink)

Show all headers | View raw


On 7/31/2013 5:16 AM, CWr wrote:
> Peter, thanks for your response.
> Sure, you are right when you say that's easier to use standard attribute assigning via __init__.
>
> But my intention was:
> - reducing the complexiticity of __init__
> - avoiding boiler-plates (mostly property descriptors inside of the main class)
> - creating instances (for complex data strings) only if they will be needed, otherwise use default instances (descriptors)
> - make it prossible that the data structure can be used in static context - like MyClass.attr - to get default values
>
> Standard procedure:
>
>>>> class C:

     DEFAULT_4_TWO = <somethint> # for following code to work

>>>>     def __init__(self, one, two=None, three=None, four=None, five=None, ...):
>>>>         if not two is None:

"if two is not None:"  reads better and is the preferred form.
'is not' is a single comparison operator, just like '!=' and 'not in'. 
The current CPython AST or peephole optimizer happens to notice that the 
'is' operator followed by the 'not' operator can be replaced by the 'is 
not' operator, but this is not guaranteed for all implementations.

>>>>             self.two = Value(two)
>>>>         else:
>>>>             self.two = Value(self.DEFAULT_4_TWO)

self.two = Value(two if two is not None else self.DEFAULT_4_TWO)

There is no need to introduce the new name DEFAULT_4_TWO. It is a 
symptom of using the wrong namespace to get the default.

class C:
     two = <default>
     def __init__(self, two=None):
         self.two = Value(two if two is not None else C.two)

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python descriptor protocol (for more or less structured data) CWr <christoph.wruck@gmail.com> - 2013-07-30 06:04 -0700
  Re: Python descriptor protocol (for more or less structured data) Peter Otten <__peter__@web.de> - 2013-07-30 16:35 +0200
    Re: Python descriptor protocol (for more or less structured data) CWr <christoph.wruck@gmail.com> - 2013-07-31 02:16 -0700
      Re: Python descriptor protocol (for more or less structured data) Terry Reedy <tjreedy@udel.edu> - 2013-07-31 13:44 -0400

csiph-web