Christian Heimes | 12 Jul 2007 17:43
Picon

C# style properties

Hello!

In the past few months I've been toying around with .NET, C# and
PythonNet. While I still think that C# is too wory (public static
explicit operator Egg(Spam spam) { ... }) C# has one syntax feature I
really like to see in Python.

private float _a
public float a
{
    get { return _a; }
    set { _a = value; }
}

I think it's a very nice way to define a variable that acts similar to
Python properties. get, set and value are part of the syntax.

Python has no nice way to define a property with set and get. You always
have to use lambda or some private methods.

class Now:
    _a = 0.0
     <at> property
    def a(self):
        """Read only property
        return self._a

    def _geta(self):
        return self._a
    def _seta(self, value):
(Continue reading)

Steven Bethard | 12 Jul 2007 18:40
Picon
Gravatar

Re: C# style properties

On 7/12/07, Christian Heimes <lists@...> wrote:
> What do you think about this syntax?
>
> class EasyExample:
>     _a = 0.0
>     property a:
>         """doc string
>         """
>         def get(self) -> float:
>             return self._a
>         def set(self, value):
>             self._a = float(value)
>         def delete(self):
>             del self._a

I'm pretty sure Guido's said before that he doesn't like the
get/set/del methods being indented more than other instance methods
would be. If you really like this style, you can get something like it
today using inner classes:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442418

Steve
--

-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy
Josiah Carlson | 12 Jul 2007 18:51
Picon
Favicon

Re: C# style properties


Christian Heimes <lists@...> wrote:
> 
> Hello!
> 
> In the past few months I've been toying around with .NET, C# and
> PythonNet. While I still think that C# is too wory (public static
> explicit operator Egg(Spam spam) { ... }) C# has one syntax feature I
> really like to see in Python.
> 
> private float _a
> public float a
> {
>     get { return _a; }
>     set { _a = value; }
> }
[snip]
> Comments?

Python has an exact equivalent to the C# method (which uses a new
block/scope for defining properties...)

    class foo:
        class a(Property):
            ''' documentation '''
            def get(self):
                return self._a
            def set(self, val):
                self._a = val
            def de1(self):
(Continue reading)

George Sakkis | 12 Jul 2007 19:20
Picon

Re: C# style properties

On 7/12/07, Steven Bethard <steven.bethard@...> wrote:

> On 7/12/07, Christian Heimes <lists@...> wrote:
> > What do you think about this syntax?
> >
> > class EasyExample:
> >     _a = 0.0
> >     property a:
> >         """doc string
> >         """
> >         def get(self) -> float:
> >             return self._a
> >         def set(self, value):
> >             self._a = float(value)
> >         def delete(self):
> >             del self._a
>
> I'm pretty sure Guido's said before that he doesn't like the
> get/set/del methods being indented more than other instance methods
> would be. If you really like this style, you can get something like it
> today using inner classes:
>
>     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442418

Or decorators: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698

George

--

-- 
"If I have been able to see further, it was only because I stood on
(Continue reading)

Georg Brandl | 12 Jul 2007 23:10
Picon
Gravatar

Re: C# style properties

Josiah Carlson schrieb:
> Christian Heimes <lists@...> wrote:
>> 
>> Hello!
>> 
>> In the past few months I've been toying around with .NET, C# and
>> PythonNet. While I still think that C# is too wory (public static
>> explicit operator Egg(Spam spam) { ... }) C# has one syntax feature I
>> really like to see in Python.
>> 
>> private float _a
>> public float a
>> {
>>     get { return _a; }
>>     set { _a = value; }
>> }
> [snip]
>> Comments?
> 
> Python has an exact equivalent to the C# method (which uses a new
> block/scope for defining properties...)
> 
>     class foo:
>         class a(Property):
>             ''' documentation '''
>             def get(self):
>                 return self._a
>             def set(self, val):
>                 self._a = val
>             def de1(self):
(Continue reading)

Christian Heimes | 13 Jul 2007 01:58
Picon

Re: C# style properties

Georg Brandl wrote:
> No, it's because they feel that the word "class" to introduce a property
> is ugly, and I can't blame them for it.

Yeah, it looks ugly, it's hard to understand if you aren't familiar with
the technique and it doesn't integrate into documentation tools like
pydoc. The trick isn't part of Python's stdlib, too.

I like to see a clean solution that is easy to understand and not a
genius but tricky metaclass hack.

Christian
Greg Ewing | 13 Jul 2007 03:40
Picon
Picon
Favicon

Re: C# style properties

Josiah Carlson wrote:
>     class foo:
>         class a(Property):
>             ''' documentation '''
>             def get(self):
>                 return self._a
>             ...
> 
> I'm curious as to why this still gets
> brought up when the obvious syntax is more or less identical to
> basically all reasonable alternate syntaxes.

I would dispute that anything involving a class statement
that creates something other than a class is "obvious".

--
Greg
Josiah Carlson | 13 Jul 2007 09:16
Picon
Favicon

Re: C# style properties


Greg Ewing <greg.ewing@...> wrote:
> Josiah Carlson wrote:
> >     class foo:
> >         class a(Property):
> >             ''' documentation '''
> >             def get(self):
> >                 return self._a
> >             ...
> > 
> > I'm curious as to why this still gets
> > brought up when the obvious syntax is more or less identical to
> > basically all reasonable alternate syntaxes.
> 
> I would dispute that anything involving a class statement
> that creates something other than a class is "obvious".

The "Property" statement that is longer, the existance of get/set/de1
methods, those didn't catch your eye?  What if the user plopped in a
nice big "__metaclass__ = Property" line?  Ugly or not, I find that it
has helped me organize (with indentation), not repeat myself, and not
have to clean up spare get/set/del methods.  I personally prefer it to
x = property(...), a property decorator, and a bazillion times more than
any property-specific syntax.

 - Josiah
Neil Toronto | 13 Jul 2007 11:43

Re: C# style properties

Josiah Carlson wrote:
> Greg Ewing <greg.ewing@...> wrote:
>   
>> Josiah Carlson wrote:
>>     
>>>     class foo:
>>>         class a(Property):
>>>             ''' documentation '''
>>>             def get(self):
>>>                 return self._a
>>>             ...
>>>
>>> I'm curious as to why this still gets
>>> brought up when the obvious syntax is more or less identical to
>>> basically all reasonable alternate syntaxes.
>>>       
>> I would dispute that anything involving a class statement
>> that creates something other than a class is "obvious".
>>     
>
> The "Property" statement that is longer, the existance of get/set/de1
> methods, those didn't catch your eye?  What if the user plopped in a
> nice big "__metaclass__ = Property" line?  Ugly or not, I find that it
> has helped me organize (with indentation), not repeat myself, and not
> have to clean up spare get/set/del methods.  I personally prefer it to
> x = property(...), a property decorator, and a bazillion times more than
> any property-specific syntax.
>   

Your original question was on how obvious this is to *invent*, but here 
(Continue reading)

Steven Bethard | 13 Jul 2007 18:01
Picon
Gravatar

Re: C# style properties

On 7/13/07, Neil Toronto <ntoronto@...> wrote:
> On a very related topic, I find it interesting how often classes get
> abused just to provide a convenient namespace to stick things in.
> Classes are heavy things, though, with a lot of semantics and
> behind-the-scenes witchery to deal with inheritance and metaclasses and
> such-like. Why bring that baggage into it? It might be nice to be able
> to declare a one-off namespace:
>
>     class Foo:
>         x:
>             '''documentation'''
>             def __get__(self, instance, owner):  # self = x, instance =
> a Foo(), owner = Foo()
>                 return instance._x
>
>
>     logger:
>         '''Singleton logger object blah blah...'''
>         db = make_db_connection(...)
>
>         def log(self, message):
>             ...

You should look at the "make" statement PEP, which offered you
something like this:

    http://www.python.org/dev/peps/pep-0359/

It was withdrawn at Guido's request - partly because he didn't like
how it allowed you to indent the methods for properties.
(Continue reading)


Gmane