[Python-ideas] Re: Decorators for class non function properties

2020-08-07 Thread Steven D'Aprano
On Fri, Aug 07, 2020 at 12:22:28PM +1200, Greg Ewing wrote: > On 7/08/20 2:47 am, David Mertz wrote: > >The only difference is that in the usual existing style, 'a' doesn't > >know that it's called "a".  You and Steven have both, basically, said > >"Why would you possibly care about that?" > > I

[Python-ideas] Re: Decorators for class non function properties

2020-08-07 Thread Steven D'Aprano
On Thu, Aug 06, 2020 at 04:01:47PM -, redrad...@gmail.com wrote: > I see lots of use-cases for property decorators ... We have had property decorators since version Python 2.2 which was 18 years ago. We know that there are many wonderful use-cases for things like this. What you are not expl

[Python-ideas] Re: Decorators for class non function properties

2020-08-07 Thread Steven D'Aprano
On Thu, Aug 06, 2020 at 04:03:39PM -, redrad...@gmail.com wrote: > No it is not possible to have something like this: > ```python > def function(cls): > # Where is cls is Neuron class object > pass > > class Neuron: > activation = function(Neuron) > ``` Correct. And it isn't pos

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Greg Ewing
On 7/08/20 2:47 am, David Mertz wrote: The only difference is that in the usual existing style, 'a' doesn't know that it's called "a".  You and Steven have both, basically, said "Why would you possibly care about that?" I've only really been thinking about attributes, but I suppose it might be

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread redradist
No it is not possible to have something like this: ```python def function(cls): # Where is cls is Neuron class object pass class Neuron: activation = function(Neuron) ``` ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread redradist
Also instead of making all class as dataclass, it would be possible to make only some properties as instance properties: ```python class Client: bank = Bank() @instance name = Name() print(f'Client.bank is {Client.bank}') client = Client() print(f'client.name is {client.name}') ``` _

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread redradist
Actually in example: ```python class MyClass: @my_property name = arg class MyClass: def name(self): ... def see_name(self): ... ``` I have done mistake ... of course it will not be like this ... What I wanted to show that @my_property could add more complex behav

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread David Mertz
On Thu, Aug 6, 2020 at 2:52 AM Greg Ewing wrote: > On 6/08/20 6:42 am, David Mertz wrote: > @unit("meter") a = 3 # a = unit("meter")("a", 3) > @unit("foot") b = 4 # b = unit("foot")("b", 4) > > This still doesn't explain why the decorator syntax would be > significantly better than j

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Guido van Rossum
Maybe I’m lacking context, but I don’t understand the proposal. Can you explain the semantics and syntax you have in mind in more detail? How do you get from the first example (@my_property etc.) to the second (def name, def set_name)? —Guido On Thu, Aug 6, 2020 at 01:13 wrote: > I think a prop

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Joao S. O. Bueno
On Thu, 6 Aug 2020 at 00:14, Guido van Rossum wrote: > On Wed, Aug 5, 2020 at 7:06 PM Steven D'Aprano > wrote: > >> *blinks* >> >> When did this happen? >> >> I'm on Python-Ideas, Python-Dev, and I get announcements of new issues >> on the bug tracker, and I don't recall ever seeing this feature

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Steven D'Aprano
On Thu, Aug 06, 2020 at 08:11:50AM -, redrad...@gmail.com wrote: > I think a property decorator can be useful, because you consider the simplest > case with: We already have `property`, which can be used as a decorator. > ```python > class MyClass: > @my_property > name = arg > ```

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Steven D'Aprano
On Thu, Aug 06, 2020 at 09:57:59AM +0200, Dominik Vilsmeier wrote: > It seems that the OP has many such transformations and wants to use > decorators to define a pipeline of transformations: > >     @foo >     @bar >     @baz >     something = initial_value > > instead of > >     something = fo

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Chris Angelico
On Thu, Aug 6, 2020 at 6:16 PM wrote: > > I think a property decorator can be useful, because you consider the simplest > case with: > ```python > class MyClass: > @my_property > name = arg > ``` > but consider it can generate the following code: > ```python > class MyClass: > def nam

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread redradist
I think a property decorator can be useful, because you consider the simplest case with: ```python class MyClass: @my_property name = arg ``` but consider it can generate the following code: ```python class MyClass: def name(self): ... def see_name(self): ... ```

[Python-ideas] Re: Decorators for class non function properties

2020-08-06 Thread Dominik Vilsmeier
On 06.08.20 04:58, Guido van Rossum wrote: On Wed, Aug 5, 2020 at 6:42 PM Steven D'Aprano mailto:st...@pearwood.info>> wrote: On Wed, Aug 05, 2020 at 06:15:22PM -0700, Guido van Rossum wrote: > On Wed, Aug 5, 2020 at 5:55 PM Steven D'Aprano mailto:st...@pearwood.info>> wrote: >

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Greg Ewing
On 6/08/20 6:42 am, David Mertz wrote: @unit("meter") a = 3  # a = unit("meter")("a", 3) @unit("foot") b = 4   # b = unit("foot")("b", 4) This still doesn't explain why the decorator syntax would be significantly better than just calling the function directly. meters = unit("meter") feet = uni

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Guido van Rossum
On Wed, Aug 5, 2020 at 7:06 PM Steven D'Aprano wrote: > *blinks* > > When did this happen? > > I'm on Python-Ideas, Python-Dev, and I get announcements of new issues > on the bug tracker, and I don't recall ever seeing this feature > discussed. > Oddly I barely recall it either, even though acco

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Guido van Rossum
On Wed, Aug 5, 2020 at 6:42 PM Steven D'Aprano wrote: > On Wed, Aug 05, 2020 at 06:15:22PM -0700, Guido van Rossum wrote: > > On Wed, Aug 5, 2020 at 5:55 PM Steven D'Aprano > wrote: > > > > That require two different rules for decorators: > > > > > > @decorator over a `class name` or `def name`

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Steven D'Aprano
On Thu, Aug 06, 2020 at 11:22:38AM +1000, Chris Angelico wrote: > On Thu, Aug 6, 2020 at 11:11 AM Steven D'Aprano wrote: > > [Dominik Vilsmeier]: > > > > That should be possible by doing `fred = my_property(42)` and defining > > > > `__set_name__` on the `my_property` class. > > > > Just because y

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Steven D'Aprano
On Wed, Aug 05, 2020 at 06:15:22PM -0700, Guido van Rossum wrote: > On Wed, Aug 5, 2020 at 5:55 PM Steven D'Aprano wrote: > > That require two different rules for decorators: > > > > @decorator over a `class name` or `def name` statement: > > > > - execute the statement > > - bind `name = decorat

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Chris Angelico
On Thu, Aug 6, 2020 at 11:11 AM Steven D'Aprano wrote: > [Dominik Vilsmeier]: > > > That should be possible by doing `fred = my_property(42)` and defining > > > `__set_name__` on the `my_property` class. > > Just because you define your own dunder method (which you shouldn't do, > since dunders ar

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Steven D'Aprano
On Wed, Aug 05, 2020 at 02:42:34PM -0400, David Mertz wrote: > Here's something I think could be more useful (again, I'm +0 at best > myself). > > >>> @unit("meter") a = 3 # a = unit("meter")("a", 3) Why does the measurement "3 metres" need to know that it is bound to the target name "a"? If

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Guido van Rossum
On Wed, Aug 5, 2020 at 5:55 PM Steven D'Aprano wrote: > On Wed, Aug 05, 2020 at 10:40:02PM +1200, Greg Ewing wrote: > > > A considerable number of moons ago, I suggested that > > > > @my_property > > fred = 42 > > > > should expand to > > > > fred = my_property("fred", 42) > > > That

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Steven D'Aprano
On Wed, Aug 05, 2020 at 03:35:15PM +0200, Marco Sulla wrote: [Greg Ewing] > > > A considerable number of moons ago, I suggested that > > > > > > @my_property > > > fred = 42 > > > > > > should expand to > > > > > > fred = my_property("fred", 42) > > > > > > The point being to give the

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Steven D'Aprano
On Wed, Aug 05, 2020 at 10:40:02PM +1200, Greg Ewing wrote: > A considerable number of moons ago, I suggested that > > @my_property > fred = 42 > > should expand to > > fred = my_property("fred", 42) That require two different rules for decorators: @decorator over a `class name`

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Greg Ewing
On 6/08/20 1:35 am, Marco Sulla wrote: I suppose that what Greg Ewing suggests is a way to define a sort of custom simple statement. you could write @print "Hello" My suggestion was only for decorating assignments to a bare name, so that wouldn't have been legal. But that was long before __s

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread André Roberge
On Wed, Aug 5, 2020 at 3:40 PM Ethan Furman wrote: > On 8/5/20 11:11 AM, Jonathan Goble wrote: > > > That's literally useless, because after running that there is nothing > > stopping you from doing: > > > > >>> a = 10 > > > > or even: > > > > >>> a = "python has no constants" > > > > And now a

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread David Mertz
On Wed, Aug 5, 2020 at 2:25 PM Jonathan Fine wrote: > Real world examples where it would be useful are generally worth much more > than invented examples. > I agree that @const is not really a useful "value decorator." I was just picking up the example that occurred up-thread. Here's something

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Ethan Furman
On 8/5/20 11:11 AM, Jonathan Goble wrote: That's literally useless, because after running that there is nothing stopping you from doing: >>> a = 10 or even: >>> a = "python has no constants" And now a has a value different from 5. There is nothing even remotely resembling const-ness to t

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread nate lust
All, " And Python can't do that, and that's probably a good thing." Johnathan, you are right, I emailed this list a year or so ago with a way to overload assignment and lookup (i.e. what happens when you >>> a) and it was discussed for a while, but ultimately there were reasons (that I largely agr

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Jonathan Fine
If you really want something like this, you can today write: >>> def wibble(fn): ... return ', '.join(fn()) >>> @wibble ... def (): ... return 'APPLES' >>> 'A, P, P, L, E, S' This allows you to use a decorator, if that's really what you want to do.

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Jonathan Goble
On Wed, Aug 5, 2020 at 2:03 PM Ricky Teachey wrote: > > And btw this works: > > >>> class const(int): > ... def __new__(cls, name, val): > ... obj = super().__new__(cls, val) > ... obj.name = name > ... return obj > ... def about(self): > ... print(self.nam

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Ethan Furman
On 8/5/20 10:54 AM, David Mertz wrote: I'm not advocating it, and I'm not the one that came up with it. But my impression is that it is intended to mean: a = const('a', 5) This doesn't seem completely pointless: class const(): ...     def __init__(self, name, val): ... self.name = name ...

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Ricky Teachey
On Wed, Aug 5, 2020 at 1:54 PM David Mertz wrote: > On Wed, Aug 5, 2020 at 1:27 PM Ricky Teachey wrote: > >> On Wed, Aug 5, 2020 at 11:41 AM Marco Sulla >> wrote: >> >>> On Wed, 5 Aug 2020 at 15:53, Ricky Teachey wrote: >>> from mypython import * >>> @const a = 5 >>> >> >> I'm probably dim bu

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread David Mertz
On Wed, Aug 5, 2020 at 1:27 PM Ricky Teachey wrote: > On Wed, Aug 5, 2020 at 11:41 AM Marco Sulla > wrote: > >> On Wed, 5 Aug 2020 at 15:53, Ricky Teachey wrote: >> from mypython import * >> @const a = 5 >> > > I'm probably dim but I have no idea what that is supposed to mean or do. > Is this

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Ricky Teachey
On Wed, Aug 5, 2020 at 11:41 AM Marco Sulla wrote: > On Wed, 5 Aug 2020 at 15:53, Ricky Teachey wrote: > > How about this? > > > > @print(sep="\n") > > 1, 2, 3 > > If you consider @print as a function decorator, it's quite a problem. > But I was thinking mainly about a sort of "decorator" for th

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Marco Sulla
On Wed, 5 Aug 2020 at 15:53, Ricky Teachey wrote: > How about this? > > @print(sep="\n") > 1, 2, 3 If you consider @print as a function decorator, it's quite a problem. But I was thinking mainly about a sort of "decorator" for the parser and/or the AST compiler. This could allow you to have, for

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Ricky Teachey
On Wed, Aug 5, 2020 at 9:38 AM Marco Sulla wrote: > On Wed, 5 Aug 2020 at 13:29, Dominik Vilsmeier > wrote: > > > > On 05.08.20 12:40, Greg Ewing wrote: > > > A considerable number of moons ago, I suggested that > > > > > > @my_property > > > fred = 42 > > > > > > should expand to > > >

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Marco Sulla
On Wed, 5 Aug 2020 at 13:29, Dominik Vilsmeier wrote: > > On 05.08.20 12:40, Greg Ewing wrote: > > A considerable number of moons ago, I suggested that > > > > @my_property > > fred = 42 > > > > should expand to > > > > fred = my_property("fred", 42) > > > > The point being to give the

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Dominik Vilsmeier
On 05.08.20 12:40, Greg Ewing wrote: On 5/08/20 9:13 pm, Serhiy Storchaka wrote: the code can be written as class Neuron: activation = linear_activation(activation) I do not see reasons to introduce special syntax for this very specific code. A considerable number of moons ago

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Greg Ewing
On 5/08/20 9:13 pm, Serhiy Storchaka wrote: the code can be written as class Neuron: activation = linear_activation(activation) I do not see reasons to introduce special syntax for this very specific code. A considerable number of moons ago, I suggested that @my_property

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread redradist
But I can do the same thing with class methods ... but anyway it was introduced method decorators to simplify development and add extra power ... ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Chris Angelico
On Wed, Aug 5, 2020 at 8:01 PM wrote: > > It could work if we extend syntax like this: > ```python > class Neuron: > activation # By default creates activation with None value\ Why not just "activation = None"? > activation = linear_activation(activation) > ``` > and then we could apply

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread redradist
It could work if we extend syntax like this: ```python class Neuron: activation # By default creates activation with None value activation = linear_activation(activation) ``` and then we could apply as may decorators as needed: ```python class Neuron: activation # By default creates act

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread Serhiy Storchaka
05.08.20 10:36, redrad...@gmail.com пише: > Decorator will do the same thing as general decorator So the code class Neuron: @linear_activation activation would be equivalent to the following code? class Neuron: activation activation = linear_activation(ac

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread redradist
Also there is maybe some addition parameter like self: ```python class Neuron: @instance_property activation def __init__(self): # automatically created pass ... def instance_property(name, property, self, *args): # Create property on instance ```

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread redradist
Decorator will do the same thing as general decorator For example it could be implemented like this: ```python class linear_activation: def __init(self, name, property): ... def linear_activation(name, property): ... ``` ___ Python-i

[Python-ideas] Re: Decorators for class non function properties

2020-08-05 Thread redradist
Disagree, because for example what if I want custom property with two or three decorators ? Like this: ```python class Neuron: @softmax_activation(weights=["w0", "w1"]) @linear_activation activation def __init__(self): self.w0 = [...] self.w1 = [...] ... ``` o

[Python-ideas] Re: Decorators for class non function properties

2020-08-04 Thread Joao S. O. Bueno
When one needs a custom property, the easy, readable, and that already works way to create then is just to create an instance of the their class and assign the instance to the desired class attribute. All the class have to do is to have the methods that identify it as a descriptor: "__get__" and "_

[Python-ideas] Re: Decorators for class non function properties

2020-08-04 Thread Chris Angelico
On Tue, Aug 4, 2020 at 6:09 PM wrote: > > Hi all, > > Seems like this topic was previously raised, but what if we add possibility > to decorate non function properties in class: > > ```python > class Neuron: > @linear_activation > activation > ``` What would that decoration do, exactly?