
Hello people, I was thinking of a "def" statement in Python. Its really weird that it can define only method, since it means "define". It shouldn't be hard to detect if this is a variable or a method, because method needs "()" to have arguments(even if there are zero of them), and defining variable value supposed to have "=" as the definition mark. Hope it would be added someday

Why would anyone want to type: def variable = value when they could just type: variable = value instead? Perhaps if I was being paid by the character typed... def my__really__good__variable = ( value ) # Assign value to my__really__good__variable. *wink* But all joking aside, what benefit would this "def" statement have? Why would I want to use it? On Sat, Oct 23, 2021 at 04:37:20PM -0000, blek blek wrote:
I was thinking of a "def" statement in Python. Its really weird that it can define only method, since it means "define".
It means *define function*, not just "define anything". We don't use def to define classes, or modules, or lists, or other objects. -- Steve

From my phone. An important thing about def x and class A is that the strings x and A are made available to the constructor for x and A respectively. The same is not true for x=val. Jonathan

Jonathan Fine writes:
What do you mean by "constructor" here? Normally that word refers to methods that populate the attributes of instances (in Python, __init__ and__new__). But functions and methods don't have such, so you must mean something else?
The same is not true for x=val.
And cannot be, since no construction is involved, just evaluation of the rhs expression, and binding of the name to the result.

24.10.21 15:20, Stephen J. Turnbull пише:
They have. Both function and type classes have constructors and they are called when a function or class is created. Values of __name__, __qualname__ and __module__ attributes are directly or indirectly passed to constructors. We do not have generalized way to call arbitrary constructor with automatically passing __name__, __qualname__ and __module__. And it would be convenient. create namedtuple Point(x, y, z=0) create enum Mode(read, write, append) create NewType UserId(int) create TypeVar T

Hi Serhiy Thank you for so clearly explaining how names get passed to function and class constructors. You also wrote:
We can already do something similar by writing (not tested) class Point(Hack): namedtuple = lambda x, y, z=0: None provided Hack has a suitable value. I don't see a way to do much better than this, without introducing a new language keyword. For example allow signature(x, y, z=0) to be an EXPRESSION that returns a function signature. By the way, class Point(Hack): def namedtuple(x, y, z=0): pass gives a syntax error at 'def'. -- Jonathan

Why would anyone want to type: def variable = value when they could just type: variable = value instead? Perhaps if I was being paid by the character typed... def my__really__good__variable = ( value ) # Assign value to my__really__good__variable. *wink* But all joking aside, what benefit would this "def" statement have? Why would I want to use it? On Sat, Oct 23, 2021 at 04:37:20PM -0000, blek blek wrote:
I was thinking of a "def" statement in Python. Its really weird that it can define only method, since it means "define".
It means *define function*, not just "define anything". We don't use def to define classes, or modules, or lists, or other objects. -- Steve

From my phone. An important thing about def x and class A is that the strings x and A are made available to the constructor for x and A respectively. The same is not true for x=val. Jonathan

Jonathan Fine writes:
What do you mean by "constructor" here? Normally that word refers to methods that populate the attributes of instances (in Python, __init__ and__new__). But functions and methods don't have such, so you must mean something else?
The same is not true for x=val.
And cannot be, since no construction is involved, just evaluation of the rhs expression, and binding of the name to the result.

24.10.21 15:20, Stephen J. Turnbull пише:
They have. Both function and type classes have constructors and they are called when a function or class is created. Values of __name__, __qualname__ and __module__ attributes are directly or indirectly passed to constructors. We do not have generalized way to call arbitrary constructor with automatically passing __name__, __qualname__ and __module__. And it would be convenient. create namedtuple Point(x, y, z=0) create enum Mode(read, write, append) create NewType UserId(int) create TypeVar T

Hi Serhiy Thank you for so clearly explaining how names get passed to function and class constructors. You also wrote:
We can already do something similar by writing (not tested) class Point(Hack): namedtuple = lambda x, y, z=0: None provided Hack has a suitable value. I don't see a way to do much better than this, without introducing a new language keyword. For example allow signature(x, y, z=0) to be an EXPRESSION that returns a function signature. By the way, class Point(Hack): def namedtuple(x, y, z=0): pass gives a syntax error at 'def'. -- Jonathan
participants (5)
-
blek blek
-
Jonathan Fine
-
Serhiy Storchaka
-
Stephen J. Turnbull
-
Steven D'Aprano