Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Greetings list.

Out of curiosity, why doesn't Python accept
def ():
return '---'

()

Where the function name is ''?

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Barry


> On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  
> wrote:
> 
> Greetings list.
> 
> Out of curiosity, why doesn't Python accept
> def ():
>return '---'
> 
> ()
> 
> Where the function name is ''?

Because there is no variable that is holding a ref to the code.
So it’s pointless.


Barry

> 
> Kind Regards,
> 
> Abdur-Rahmaan Janhangeer
> about  | blog
> 
> github 
> Mauritius
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Dan Stromberg
On Sun, Feb 20, 2022 at 7:33 AM Abdur-Rahmaan Janhangeer <
[email protected]> wrote:

> Greetings list.
>
> Out of curiosity, why doesn't Python accept
> def ():
> return '---'
>
> ()
>
> Where the function name is ''?
>

() is already an empty tuple.  It would break code to change this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 02:33, Abdur-Rahmaan Janhangeer
 wrote:
>
> Greetings list.
>
> Out of curiosity, why doesn't Python

This is often the wrong question. The question is more: Why should Python?

Python doesn't do things just because there's no reason not to.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Thanks for the answers.

@Chris Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Yes I know about lambdas but was just an introspection about
the reasoning behind ^^

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Dieter Maurer
Abdur-Rahmaan Janhangeer wrote at 2022-2-20 19:32 +0400:
>Out of curiosity, why doesn't Python accept
>def ():
>return '---'
>
>()
>
>Where the function name is ''?

Python knows about (somewhat restricted) anonymous functions:
it calls them `lambda` expressions (the body of those functions
can only be an expression).

Your example above can be expressed as
`(lambda: '---')()`.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Long running process - how to speed up?

2022-02-20 Thread Shaozhong SHI
On Sat, 19 Feb 2022 at 19:44, Mats Wichmann  wrote:

> On 2/19/22 05:09, Shaozhong SHI wrote:
> > Can it be divided into several processes?
> > Regards,
> > David
>
> The answer is: "maybe".  Multiprocessing doesn't happen for free, you
> have to figure out how to divide the task up, requiring thought and
> effort. We can't guess to what extent the problem you have is amenable
> to multiprocessing.
>
> Google for "dataframe" and "multiprocessing" and you should get some
> hits (in my somewhat limited experience in this area, people usually
> load the csv data into Pandas before they get started working with it).
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list


I am trying this approach,

import multiprocessing as mp

def my_func(x):
  print(x**x)

def main():
  pool = mp.Pool(mp.cpu_count())
  result = pool.map(my_func, [4,2,3])

if __name__ == "__main__":
  main()

I modified the script and set off a test run.

However, I have no idea whether this approach will be faster than
conventional approach.

Any one has idea?

Regards,

David
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Long running process - how to speed up?

2022-02-20 Thread Shaozhong SHI
On Sat, 19 Feb 2022 at 18:51, Alan Gauld  wrote:

> On 19/02/2022 11:28, Shaozhong SHI wrote:
>
> > I have a cvs file of 932956 row
>
> That's not a lot in modern computing terms.
>
> > and have to have time.sleep in a Python
> > script.
>
> Why? Is it a requirement by your customer? Your manager?
> time.sleep() is not usually helpful if you want to do
> things quickly.
>
> > It takes a long time to process.
>
> What is a "long time"? minutes? hours? days? weeks?
>
> It should take a million times as long as it takes to
> process one row. But you have given no clue what you
> are doing in each row.
> - reading a database?
> - reading from the network? or the internet?
> - writing to a database? or the internet?
> - performing highly complex math operations?
>
> Or perhaps the processing load is in analyzing the totality
> of the data after reading it all? A very different type
> of problem. But we just don't know.
>
> All of these factors will affect performance.
>
> > How can I speed up the processing?
>
> It all depends on the processing.
> You could try profiling your code to see where the time is spent.
>
> > Can I do multi-processing?
>
> Of course. But there is no guarantee that will speed things
> up if there is a bottleneck on a single resource somewhere.
> But it might be possible to divide and conquer and get better
> speed. It all depends on what you are doing. We can't tell.
>
> We cannot answer such a vague question with any specific
> solution.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
> --
> https://mail.python.org/mailman/listinfo/python-list


Do not know these answers yet.  Now, it appeared to hang/stop at a point
and does not move on.

Regards,

David
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Long running process - how to speed up?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 05:07, Shaozhong SHI  wrote:
> However, I have no idea whether this approach will be faster than
> conventional approach.
>
> Any one has idea?

Try it. Find out. The only way to know is to measure.

I can't see the sleep call though. You may need to post your actual
code instead of trivial examples.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Avi Gross via Python-list
Not really. Anonymous functions are anonymous in name only, so to speak.
When you call a function and pass it an argument that is an anonymous function 
definition, it is bound to a variable within the function and used mostly in a 
non-anonymous way. You did not bother giving it a name but it is referenced and 
retains an existence until it goes out of scope. Not having a name within your 
own scope means you do not have to do anything like deleting it further in your 
code. But most code that uses anonymous functions could be rewritten to use a 
named function. 
However, a compiler or interpreter may well be optimized to decide to ignore or 
even rewrite things. Consider what it might do to something like "if (5 < 4) 
..." where all the code in the ellipsis can never be reached and hence it and 
the entire statement can be silently removed or ignored OR perhaps reported as 
a logical error. The code is absolutely valid in terms of syntax, but not valid 
as having any purpose. But what if all the above is the body of another 
statement like an "else" which would become a problem if you removed this code? 
Obviously you might have to walk up the tree and trim more.
Declaring an anonymous function in a context where it attaches to no names 
anywhere can be allowed to proceed, if that is important to anyone, but garbage 
collection will rapidly reclaim the space, I would think. So why bother 
allocating the space? And note if the allocation method is odd and leaves it 
entirely out of the tables that keep track, you then would have a memory leak.


-Original Message-
From: Abdur-Rahmaan Janhangeer 
To: Chris Angelico 
Cc: Python 
Sent: Sun, Feb 20, 2022 12:27 pm
Subject: Re: Why does not Python accept functions with no names?

Thanks for the answers.

@Chris Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Long running process - how to speed up?

2022-02-20 Thread Avi Gross via Python-list
dAVId,
I would like to assume that the processing needed is quite a bit more than 
calculating the square of each X.
But as we are getting negligible information on anything useful, why continue 
interacting. 
I am trying to imagin a scenario with a million rows of sorts in a CSV 
(typically not used for a single column of data, as that tends not to use 
commas) where you read in the data and want to generate just the square of each 
number in what may be a disorganized order as threads are not guaranteed to do 
anything but interleave!
Parallelism can be used in cases where the data is segmented properly and the 
algorithm adjusted to fit the needs. But the overhead can be substantial. For 
the  trivial task mentioned, which I have to hope is not the actual task, you 
can get quite a decent speed by reading it into a numpy data structure and 
using a vectorized way to produce the squares and simply print that. 
The original question here is turning out to be mysterious as it began by 
asking how to speed up some slow process not yet explained and some mumbling 
about calling a sleep function. I note some parallel algorithms require a 
variant of that in that some parts must wait for other parts to complete and 
arrange to be dormant till signaled or schedule themselves to be woken 
regularly and check if things are ready for them to resume. Sleeping is a very 
common occurence in systems that are time-shared.


-Original Message-
From: Shaozhong SHI 
To: Mats Wichmann 
Cc: [email protected]
Sent: Sun, Feb 20, 2022 1:05 pm
Subject: Re: Long running process - how to speed up?

On Sat, 19 Feb 2022 at 19:44, Mats Wichmann  wrote:

> On 2/19/22 05:09, Shaozhong SHI wrote:
> > Can it be divided into several processes?
> > Regards,
> > David
>
> The answer is: "maybe".  Multiprocessing doesn't happen for free, you
> have to figure out how to divide the task up, requiring thought and
> effort. We can't guess to what extent the problem you have is amenable
> to multiprocessing.
>
> Google for "dataframe" and "multiprocessing" and you should get some
> hits (in my somewhat limited experience in this area, people usually
> load the csv data into Pandas before they get started working with it).
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list


I am trying this approach,

import multiprocessing as mp

def my_func(x):
  print(x**x)

def main():
  pool = mp.Pool(mp.cpu_count())
  result = pool.map(my_func, [4,2,3])

if __name__ == "__main__":
  main()

I modified the script and set off a test run.

However, I have no idea whether this approach will be faster than
conventional approach.

Any one has idea?

Regards,

David
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Eryk Sun
On 2/20/22, Abdur-Rahmaan Janhangeer  wrote:
>
> Out of curiosity, why doesn't Python accept
> def ():
> return '---'

The `def` keyword is compiled as an assignment statement, not an
expression that evaluates anonymously on the stack. Using `def` as an
expression would require new syntax. For example, maybe something like
the following:

funcs[i] = def (x, *, y=0) := (
if x < y:
return spam(x)
return eggs(x)
)

Since parsing Python depends on white space, if a `def (...) :=`
expression is multiline, the first statement on a new line would set
the indentation level, up to but not including the closing
parenthesis. (Style guides would likely restrict what's considered
good form.) The anonymous function could be called immediately. For
example:

results[i] = def (x, *, y=0) := (
if x < y:
return spam(x)
return eggs(x)
)(z)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 09:38, Eryk Sun  wrote:
>
> On 2/20/22, Abdur-Rahmaan Janhangeer  wrote:
> >
> > Out of curiosity, why doesn't Python accept
> > def ():
> > return '---'
>
> The `def` keyword is compiled as an assignment statement, not an
> expression that evaluates anonymously on the stack. Using `def` as an
> expression would require new syntax. For example, maybe something like
> the following:
>
> funcs[i] = def (x, *, y=0) := (
> if x < y:
> return spam(x)
> return eggs(x)
> )
>

There's always decorators.

>>> @functools.partial(operator.setitem, funcs, i)
... def _(x, *, y=0):
... if x < y:
... return spam(x)
... return eggs(x)
...
>>> funcs[i]


(Has the side effect of setting _ to None.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Christian Gollwitzer

Am 20.02.22 um 16:48 schrieb Python:

Abdur-Rahmaan Janhangeer wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
 return '---'

()

Where the function name is ''?


For the same reason an empty sequence of characters cannot
be a variable name. Do you know any language (or formal
theory) that allows that?


Tcl allows that:

Main console display active (Tcl8.6.9 / Tk8.6.9)
(CDEF) 49 % set "" Hallo
Hallo
(CDEF) 50 % puts ${}
Hallo
(CDEF) 51 % proc "" {} { puts "I'm empty" }
(CDEF) 52 % ""
I'm empty
(CDEF) 53 %

Any string can be a variable or command name, only :: is special as a 
namespace separator.


This only works because of the sparse syntax; to retrieve a variable's 
content, $ is used. For "strange" names quoting is required, therefore I 
had to use "" in the example.


It's a different matter how useful this actually is. One of the object 
systems in Tcl uses the empty variable to represent "self" as an array, 
so that you can write $(prop) for self.prop as it is in Python.


Christian


--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Python

Abdur-Rahmaan Janhangeer wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
 return '---'

()

Where the function name is ''?


For the same reason an empty sequence of characters cannot
be a variable name. Do you know any language (or formal
theory) that allows that?


--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Python

Barry wrote:




On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
return '---'

()

Where the function name is ''?


Because there is no variable that is holding a ref to the code.
So it’s pointless.


Fun fact, it can be done, but it's (afaik) then (almost) unusable...

>>> a
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'a' is not defined
>>> locals()['a'] = 42
>>> a
42
>>> locals()[''] = 42
>>> locals()['']
42
>>> locals()[''] = (lambda x: x*42)
>>> locals()[''](1)
42

--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Greg Ewing

On 21/02/22 6:27 am, Abdur-Rahmaan Janhangeer wrote:

Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.


The syntax for a function definition is defined in the grammar
as requiring an identifier. An identifier in turn is defined
as consisting of at least one character. So the grammar would
need to be changed to make the name optional. Also, the
compiler would need a special case to treat a missing name
there as though it were an empty string.

So again, why *should* it be allowed, given that the parser and
compiler would have to go out of their way to treat it as a
special case, only to create a function that there is no
easy way to call?

BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Long running process - how to speed up?

2022-02-20 Thread Dennis Lee Bieber
On Sun, 20 Feb 2022 18:05:33 +, Shaozhong SHI 
declaimed the following:


>I am trying this approach,
>
>import multiprocessing as mp
>
>def my_func(x):
>  print(x**x)
>

Not much of a processing load there, especially for your small set of
integers. I suspect you are consuming a significant time just having the OS
create and tear down each process.

In truth, for that example, I suspect plain threads will run faster
because you don't have the overhead of setting up a process with new
stdin/stdout/stderr. The exponential operation probably runs within one
Python threading quantum, and the print() will trigger a thread switch to
allow the next one to compute.

>def main():
>  pool = mp.Pool(mp.cpu_count())
>  result = pool.map(my_func, [4,2,3])

-=-=-
>>> import multiprocessing as mp
>>> mp.cpu_count()
8
>>> 
-=-=-

Really under-loaded on my Win10 system (hyperthreaded processors count
as 2 CPUs, so a quad-core HT reports as 8 CPUs).Even an older
Raspberry-Pi 3B (quad core) reports

-=-=-
md_admin@microdiversity:~$ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing as mp
>>> mp.cpu_count()
4
>>> exit()
md_admin@microdiversity:~$
-=-=-


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 14:36, Python  wrote:
>
> Barry wrote:
> >
> >
> >> On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  
> >> wrote:
> >>
> >> Greetings list.
> >>
> >> Out of curiosity, why doesn't Python accept
> >> def ():
> >> return '---'
> >>
> >> ()
> >>
> >> Where the function name is ''?
> >
> > Because there is no variable that is holding a ref to the code.
> > So it’s pointless.
>
> Fun fact, it can be done, but it's (afaik) then (almost) unusable...
>
>  >>> a
> Traceback (most recent call last):
>File "", line 1, in 
> NameError: name 'a' is not defined
>  >>> locals()['a'] = 42
>  >>> a
> 42
>  >>> locals()[''] = 42
>  >>> locals()['']
> 42
>  >>> locals()[''] = (lambda x: x*42)
>  >>> locals()[''](1)
> 42
>

Minor nitpick: Please use globals() rather than locals() for this sort
of demo. At module level (including the REPL), they are the same, but
inside a function there's no guarantee that locals() can be mutated in
this way.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Grant Edwards
On 2022-02-20, Christian Gollwitzer  wrote:

>> For the same reason an empty sequence of characters cannot
>> be a variable name. Do you know any language (or formal
>> theory) that allows that?
>
> Tcl allows that:

Interesting to know, but the fact that Tcl does something differnt is
more of an argument against it.

My one expereince trying to write a non-trivial application was a
study in frustration.  After spending days trying to get Tcl to do
something useful, I finally gave up and wrote the program in Scheme in
a few hours.

--
Grant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Avi Gross via Python-list
Amusingly, Greg, if you had a system where the un-named anonymous function was 
to be named the unique value of the empty string, then a second such anonymous 
function definition would over-write it, as with any named function. The kind 
of anonymous function we are more used to might be something you can create as 
say elements of a list so you have a list of functions you can access as in 
f[0] but in a sense that has a name as it can be accessed as a component of the 
data structure called f.  I am not sure if python would trivially let you 
create that. But the point is if you want to be able to make many such 
pseudo-anonymous functions, in the end, there can only be one.


-Original Message-
From: Greg Ewing 
To: [email protected]
Sent: Sun, Feb 20, 2022 5:17 pm
Subject: Re: Why does not Python accept functions with no names?

On 21/02/22 6:27 am, Abdur-Rahmaan Janhangeer wrote:
> Well Python deliberately throws an exception if we do not
> pass in a function name. Just wanted to know why it should. As the
> above case is a 100% pure anonymous function.

The syntax for a function definition is defined in the grammar
as requiring an identifier. An identifier in turn is defined
as consisting of at least one character. So the grammar would
need to be changed to make the name optional. Also, the
compiler would need a special case to treat a missing name
there as though it were an empty string.

So again, why *should* it be allowed, given that the parser and
compiler would have to go out of their way to treat it as a
special case, only to create a function that there is no
easy way to call?

BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.

-- 
Greg
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 16:48, Avi Gross via Python-list
 wrote:
>
> Amusingly, Greg, if you had a system where the un-named anonymous function 
> was to be named the unique value of the empty string, then a second such 
> anonymous function definition would over-write it, as with any named 
> function. The kind of anonymous function we are more used to might be 
> something you can create as say elements of a list so you have a list of 
> functions you can access as in f[0] but in a sense that has a name as it can 
> be accessed as a component of the data structure called f.  I am not sure if 
> python would trivially let you create that. But the point is if you want to 
> be able to make many such pseudo-anonymous functions, in the end, there can 
> only be one.
>

Functions in Python have two different concepts of "name", which
aren't always the same. One is the way that you refer to it; the other
is its __name__ attribute (and related attributes like __qualname__).
An anonymous function doesn't necessarily have to lack a name, per se;
it simply doesn't have a very interesting name:

>>> (lambda: 1).__name__
''
>>> (lambda: 2).__name__
''
>>> (lambda: 3).__name__
''

If you want to refer to them as f[0] etc, they can have arbitrary
names, which may or may not themselves be meaningful. I frequently
build a dictionary or list of functions by decorating standard 'def'
statements:

funcs = []

@funcs.append
def spam(): ...
@funcs.append
def ham(): ...

They still have __name__ attributes, but it's absolutely fine to
double them up, since they're going to be looked up via the list.
(Though there's still value in having at least *some* name on them,
since it shows up in backtraces.)

There's very little reason to have a function that actually doesn't
have a name. It's usually fine to give it an arbitrary and meaningless
name.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Eryk Sun
On 2/20/22, Greg Ewing  wrote:
>
> BTW, this is not what is usually meant by the term "anonymous
> function". An anonymous function is one that is not bound
> to *any* name. The thing you're proposing wouldn't be
> anonymous -- it would have a name, that name being the empty
> string.

Sorry. I read Avi's reply first, scanned the original post and didn't
consider the literal question. I doubt that Python would have become a
popular language if it allowed the empty string as an identifier. That
would let common syntax errors slip past the compiler with useless
results, e.g `= 1`, `c. = 1`. It's no less absurd in a `def`
statement.
-- 
https://mail.python.org/mailman/listinfo/python-list