Re: Function returns old value
Am 17.12.20 um 21:22 schrieb Michael F. Stemper: On 17/12/2020 03.57, Peter J. Holzer wrote: On 2020-12-17 03:06:32 -, Bischoop wrote: pasting from my IDE to vim/slrn was messing syntax, You can :set paste in vim to prevent it from messing with pasted content (don't forget to set nopaste afterwards). What's the difference between that and :set ai and :set noai With newer vims that's rarely necessary though since they can distinguish between input that was pasted and input that was typed. I thought that I was going nuts when I encountered that. Any idea how to defeat such a so-called 'feature"? Use "gvim" instead of "vim" in a Terminal. The problems arises because vim in the terminal simply gets the input and "thinks" that you typed that stuff in, for which it does auto-indenting. gvim, OTOH, doe sthe pasting by itself. It also has other useful features like menus and popup-dialogs for searching etc. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Lambda in parameters
The Question: # --- This problem was asked by Jane Street. cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4. Given this implementation of cons: def cons(a, b): def pair(f): return f(a, b) return pair Implement car and cdr. # --- Kind Regards, Abdur-Rahmaan Janhangeer https://www.github.com/Abdur-RahmaanJ Mauritius sent from gmail client on Android, that's why the signature is so ugly. On Fri, 18 Dec 2020, 10:02 Cameron Simpson, wrote: > On 17Dec2020 23:52, Abdur-Rahmaan Janhangeer wrote: > >Here is a famous question's solution > > These look like implementations of Lisp operators, which I've never > found easy to remember. So I'll do this from first principles, but > looking at the (uncommented) code. > > This is some gratuitiously tricky code. Maybe that's needed for these > operators. But there's a lot here, so let's unpick it: > > >def cons(a, b): > >def pair(f): > >return f(a, b) > >return pair > > Cons returns "pair", a function which itself accepts a function "f", > calls it with 2 values "a, b" and returns the result. For ultra > trickiness, those 2 values "a, b" are the ones you passed to the initial > call to cons(). > > This last bit is a closure: when you define a function, any nonlocal > variables (those you use but never assign to) have the defining scope > available for finding them. So the "pair" function gets "a" and "b" from > those you passed to "cons". > > Anyway, cons() returns a "pair" function hooked to the "a" and "b" you > called it with. > > >def car(c): > >return c(lambda a, b: a) > > The function accepts a function, and calls that function with "lambda a, > b: a", which is itself a function which returns its first argument. You > could write car like this: > > def car(c): > def first(a, b): > return a > return c(first) > > The lambda is just a way to write a simple single expression function. > > >print(cons(1, 2)(lambda a, b: a)) > > What is "cons(1,2)". That returns a "pair" function hooked up to the a=1 > and b=2 values you supplied. And the pair function accepts a function of > 2 variables. > > What does this do? > > cons(1, 2)(lambda a, b: a) > > This takes the function returns by cons(1,2) and _calls_ that with a > simple function which accepts 2 values and returns the first of them. > > So: > > cons(1,2) => "pair function hooked to a=1 and b=2" > > Then call: > > pair(lambda a, b: a) > > which sets "f" to the lambda function, can calls that with (a,b). So it > calls the lambda function with (1,2). Which returns 1. > > >print(car(cons(1, 2))) > > The "car" function pretty much just embodies the call-with-the-lambda. > > >but i don't understand how lambda achieves this > > If you rewite the lambda like this: > > def a_from_ab(a,b): > return a > > and then rewrite the first call to cons() like this: > > cons(1,2)(a_from_ab) > > does it make any more sense? > > Frankly, I think this is a terrible way to solve this problem, whatever > the problem was supposed to be - that is not clear. > > On the other hand, I presume it does implement the Lisp cons and car > functions. I truly have no idea, I just remember these names from my > brief brush with Lisp in the past. > > Cheers, > Cameron Simpson > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Review, suggestion etc?
Grant Edwards writes: > On 2020-12-18, Joe Pfeiffer wrote: > >> Recursion has very limited application, but where it's the right >> tool it's invaluable (top-down parsers, some graph algorithms...). >> We teach it primarily because by the time a student has a good >> handle on how to write a recursive function they understand >> functions in general really well. > > Yep, there are definitly cases where it's pretty much the only right > answer. If you try to avoid it, you end up writing what turns into a > simulation of recursion -- and doing that correctly isn't easy. Decades ago I had to write a binary tree search in FORTRAN IV. It wasn't pretty. -- https://mail.python.org/mailman/listinfo/python-list
Re: Lambda in parameters
> On 18 Dec 2020, at 14:23, Abdur-Rahmaan Janhangeer > wrote: > > The Question: > > # --- > This problem was asked by Jane Street. > > cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first > and last element of that pair. For example, car(cons(3, 4)) returns 3, and > cdr(cons(3, 4)) returns 4. > > Given this implementation of cons: > > def cons(a, b): >def pair(f): >return f(a, b) >return pair > > Implement car and cdr. Why car and cdr? Well obviously car is content of the address register and cdr is content of data register. Apparently an artefact of a early implementation of lisp. Barry > # --- > > Kind Regards, > > > Abdur-Rahmaan Janhangeer > > https://www.github.com/Abdur-RahmaanJ > > Mauritius > > sent from gmail client on Android, that's why the signature is so ugly. > >> On Fri, 18 Dec 2020, 10:02 Cameron Simpson, wrote: >> >>> On 17Dec2020 23:52, Abdur-Rahmaan Janhangeer wrote: >>> Here is a famous question's solution >> >> These look like implementations of Lisp operators, which I've never >> found easy to remember. So I'll do this from first principles, but >> looking at the (uncommented) code. >> >> This is some gratuitiously tricky code. Maybe that's needed for these >> operators. But there's a lot here, so let's unpick it: >> >>> def cons(a, b): >>> def pair(f): >>> return f(a, b) >>> return pair >> >> Cons returns "pair", a function which itself accepts a function "f", >> calls it with 2 values "a, b" and returns the result. For ultra >> trickiness, those 2 values "a, b" are the ones you passed to the initial >> call to cons(). >> >> This last bit is a closure: when you define a function, any nonlocal >> variables (those you use but never assign to) have the defining scope >> available for finding them. So the "pair" function gets "a" and "b" from >> those you passed to "cons". >> >> Anyway, cons() returns a "pair" function hooked to the "a" and "b" you >> called it with. >> >>> def car(c): >>> return c(lambda a, b: a) >> >> The function accepts a function, and calls that function with "lambda a, >> b: a", which is itself a function which returns its first argument. You >> could write car like this: >> >>def car(c): >>def first(a, b): >>return a >>return c(first) >> >> The lambda is just a way to write a simple single expression function. >> >>> print(cons(1, 2)(lambda a, b: a)) >> >> What is "cons(1,2)". That returns a "pair" function hooked up to the a=1 >> and b=2 values you supplied. And the pair function accepts a function of >> 2 variables. >> >> What does this do? >> >>cons(1, 2)(lambda a, b: a) >> >> This takes the function returns by cons(1,2) and _calls_ that with a >> simple function which accepts 2 values and returns the first of them. >> >> So: >> >>cons(1,2) => "pair function hooked to a=1 and b=2" >> >> Then call: >> >>pair(lambda a, b: a) >> >> which sets "f" to the lambda function, can calls that with (a,b). So it >> calls the lambda function with (1,2). Which returns 1. >> >>> print(car(cons(1, 2))) >> >> The "car" function pretty much just embodies the call-with-the-lambda. >> >>> but i don't understand how lambda achieves this >> >> If you rewite the lambda like this: >> >>def a_from_ab(a,b): >>return a >> >> and then rewrite the first call to cons() like this: >> >>cons(1,2)(a_from_ab) >> >> does it make any more sense? >> >> Frankly, I think this is a terrible way to solve this problem, whatever >> the problem was supposed to be - that is not clear. >> >> On the other hand, I presume it does implement the Lisp cons and car >> functions. I truly have no idea, I just remember these names from my >> brief brush with Lisp in the past. >> >> Cheers, >> Cameron Simpson >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
RE: setuptools issue
On Fri, 18 Dec 2020, Joseph L. Casale wrote: I just ran into this recently, I don't recall the actual source but it was the version of setuptools having been so old. Your version is from Jun 3, 2016... Update it, that was what worked for me. jlc, Upgraded to python-setuptools-51.0.0-x86_64. Now when I try building python3-babel it fails at this point: running import_cldr Extracting CLDR to '/tmp/SBo/babel-2.8.1/cldr/cldr-core-36' Traceback (most recent call last): File "/tmp/SBo/babel-2.8.1/scripts/import_cldr.py", line 34, in from babel import dates, numbers File "/tmp/SBo/babel-2.8.1/babel/dates.py", line 23, in import pytz as _pytz ModuleNotFoundError: No module named 'pytz' ## N.B.: pytz is installed here: pytz-2018.3-x86_64-1_SBo ## Could this also be too old? There's a pytz-2020.4 on PyPI Traceback (most recent call last): File "/tmp/SBo/babel-2.8.1/scripts/download_import_cldr.py", line 104, in main() File "/tmp/SBo/babel-2.8.1/scripts/download_import_cldr.py", line 97, in main subprocess.check_call([ File "/usr/lib64/python3.9/subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', '/tmp/SBo/babel-2.8.1/scripts/import_cldr.py', '/tmp/SBo/babel-2.8.1/cldr/cldr-core-36/common']' returned non-zero exit status 1. Traceback (most recent call last): File "/tmp/SBo/babel-2.8.1/setup.py", line 30, in setup( File "/usr/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup return distutils.core.setup(**attrs) File "/usr/lib64/python3.9/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/lib64/python3.9/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/usr/lib64/python3.9/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/tmp/SBo/babel-2.8.1/setup.py", line 27, in run subprocess.check_call([sys.executable, 'scripts/download_import_cldr.py']) File "/usr/lib64/python3.9/subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', 'scripts/download_import_cldr.py']' returned non-zero exit status 1. Regards, Rich -- https://mail.python.org/mailman/listinfo/python-list
Re: Lambda in parameters
On Friday, 18 December 2020 at 15:20:59 UTC+1, Abdur-Rahmaan Janhangeer wrote: > The Question: > > # --- > This problem was asked by Jane Street. > > cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first > and last element of that pair. For example, car(cons(3, 4)) returns 3, and > cdr(cons(3, 4)) returns 4. > > Given this implementation of cons: > def cons(a, b): > def pair(f): > return f(a, b) > return pair > Implement car and cdr. > # --- Notice that you don't need (Python) lambdas to code it, plain function definitions are fine: # --- def cons(a, b): def pair(f): return f(a, b) return pair def car(pair): def left(a, b): return a return pair(left) pair = cons(1, 2) assert car(pair) == 1 # --- That said, few basic comments: In Python, that 'cons' does not construct a pair, it rather returns a function with values a and b in its closure that, given some function, applies it to those values. In fact, Python has tuples built-in, how to build them as well as how to access their members. I take it the point of the exercise is how to use a purely functional language, such as here a fragment of Python, to encode (i.e. formalize) pairs and their operations. Julio -- https://mail.python.org/mailman/listinfo/python-list
Re: Review, suggestion etc?
On 2020-12-18, Joe Pfeiffer wrote: > Grant Edwards writes: > >> Yep, there are definitly cases where it's pretty much the only right >> answer. If you try to avoid it, you end up writing what turns into a >> simulation of recursion -- and doing that correctly isn't easy. > > Decades ago I had to write a binary tree search in FORTRAN IV. It > wasn't pretty. I saw somebody try to write a graph search in GW-BASIC once. It was a huge mess, and wasn't anywhere close to working. Doing the same thing recursively was trivial -- even in C. In Python it wuld have been even trivialler. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Lambda in parameters
On 2020-12-18, Barry wrote: >> Implement car and cdr. > Why car and cdr? > > Well obviously car is content of the address register and cdr is content of > data register. > Apparently an artefact of a early implementation of lisp. While car and cdr are lisp operators, the "content of address register" and "content of data register" etymology is apparently apocryphal: https://en.wikipedia.org/wiki/CAR_and_CDR#Etymology -- Grant -- https://mail.python.org/mailman/listinfo/python-list
"Worst bugs" and Python?
TechRepublic have published a lovely piece of 'click-bait' featuring alarmist claims such as "open-source libraries are increasingly untrustworthy" whilst trotting-out tired, old, memes and bias. Don't panic - hold-on to your PyPi! <<< The worst bugs in the top programming languages by Brandon Vigliarolo in Security on December 17, 2020, 9:32 AM PST A heatmap shows PHP has the most flaws followed by C++, then Java, .Net, JavaScript, and Python in Veracode's annual security report. >>> https://www.techrepublic.com/article/the-worst-bugs-in-the-top-programming-languages/ Does anyone think that code is 'bug free'? That's a 'filler topic' for any columnist lacking fresh ideas and desperate to fill a publishing deadline. The basis is "State of Software Security v11" 'report' produced by Veracode (https://www.veracode.com/state-of-software-security-report). You will not be surprised to note that Veracode is in the business of marketing test and analysis software. Any such report is inherently useful. They serve to ensure that we do not become complacent in our attitude to security. However, there are more "bugs" in software than fit under the heading of 'security'. Similarly, at times the report appears to lump-together C, C++, and C#; whereas at others they may not; which makes it difficult to generalise or analyse. In the same vein, infographics look nice, but what does "Code Quality" really mean? Another observation is that many of their 'categories' apply mainly to the on-line world. Corporation-only applications are protected by network defences rather than by their own devices. A more interesting figure, which is under-reported both in the article and within Veracode's summaries, is the period of vulnerability - how long it takes to fix a bug after it has been reported - and preferably with the 'danger' of the bug factored-in. Thus a bug which doesn't allow the addition of new user-credentials is quite a different matter from one which allows existing users to upgrade themselves to 'super-user'. Such analysis is possibly available, but not in the summaries (above). A quick dip into Veracode's 'vulnerability database' yielded the following intelligence: Top three "library artefacts" with Python as [the only] keyword: - firefox - thunderbird - linux-rt Is Python 'counted' in these cases because it is involved somewhere within the package, because it is the majority-language used, because it is the only language employed, or because its use contributes to most of the faults-found? Finally, such reports are primarily marketing tools, and thus notorious for bias or superficial content. Veracode do not declare the range, or limits on the range, of software they've analysed. Companies such as Microsoft and Oracle (plus, plus, ...) do not allow just-anyone to analyse their source-code - whereas 'open source' is available for analysis, by definition! An easy 'target' for shallow analysis? At this point I gave up, lacking the interest to fill-out the contact-form, or to read the entire report. The good news is, that of the six languages headlined in the summaries, Python comes-off 'best' (cf .Net, C++, Java, JavaScript, and PHP). -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
RE: dict.get(key, default) evaluates default even if key exists
Yes. In order to call D.get( ) it needs to pass two arguments. The first is 'a', simple. The second is the result of a call to get_default(). So, that is called. From INSIDE get_default() it prints 'Nobody expects this' but you should expect it, get_default() gets executed. Following that it prints '1', because the default value was NOT USED. If it was used, you would see 'Nobody expects this' followed by 0. --- Joseph S. -Original Message- From: Mark Polesky Sent: Tuesday, December 15, 2020 12:07 PM To: [email protected] Subject: dict.get(key, default) evaluates default even if key exists Hi. # Running this script D = {'a':1} def get_default(): print('Nobody expects this') return 0 print(D.get('a', get_default())) # ...generates this output: Nobody expects this 1 ### Since I'm brand new to this community, I thought I'd ask here first... Is this worthy of a bug report? This behavior is definitely unexpected to me, and I accidentally coded an endless loop in a mutual recursion situation because of it. Calling dict.get.__doc__ only gives this short sentence: Return the value for key if key is in the dictionary, else default. Nothing in that docstring suggests that the default value is evaluated even if the key exists, and I can't think of any good reason to do so. Am I missing something? Thanks, Mark -- https://mail.python.org/mailman/listinfo/python-list
Re: "Worst bugs" and Python?
On Sat, 19 Dec 2020, dn via Python-list wrote: Companies such as Microsoft and Oracle (plus, plus, ...) do not allow just-anyone to analyse their source-code - whereas 'open source' is available for analysis, by definition! An easy 'target' for shallow analysis? Looks bass-ackwards to me. That's the same argument made by proprietary companies (especially Micro$oft) in the early 1990s. What falsifies the writer's argument is all the malware that's affected Windoze over the past couple of decades compared to F/OSS OSes and applications. Sigh. Fake news hits the software industry. Carpe weekend, Rich -- https://mail.python.org/mailman/listinfo/python-list
