Re: How to set custom locals for function call?

2020-09-01 Thread Andras Tantos
On 9/1/20 5:31 PM, Chris Angelico wrote: On Wed, Sep 2, 2020 at 10:23 AM Andras Tantos wrote: I did see these macros in the CPython source code. What it seems to imply is that if I wanted to do what I intend, that is to hook every local variable assignment, I would have to modify the AST. That

Re: How to set custom locals for function call?

2020-09-01 Thread Chris Angelico
On Wed, Sep 2, 2020 at 11:29 AM Andras Tantos wrote: > OK, and sorry about that. May third time a charm? Thank you! Whatever you did this time, that worked :) > Indeed, I'm executing a code object and not a string. Is there any way > to get to the string version of the function and force a re-co

Re: How to set custom locals for function call?

2020-09-01 Thread Andras Tantos
Snipping old parts... On 9/1/20 4:01 PM, Chris Angelico wrote: On Wed, Sep 2, 2020 at 8:44 AM Andras Tantos wrote: While I'm sure you're right, it certainly is well hidden: ... In other words, I can indeed change the value of a local variable through the locals() dict. Not quite. What you'r

Re: How to set custom locals for function call?

2020-09-01 Thread Chris Angelico
On Wed, Sep 2, 2020 at 10:23 AM Andras Tantos wrote: > I did see these macros in the CPython source code. What it seems to > imply is that if I wanted to do what I intend, that is to hook every > local variable assignment, I would have to modify the AST. That seems > rather more effort than simply

Re: How to set custom locals for function call?

2020-09-01 Thread Andras Tantos
On 9/1/20 1:42 PM, Chris Angelico wrote: On Wed, Sep 2, 2020 at 5:00 AM Andras Tantos wrote: All, I'm new here, so please direct me to the right forum, if this is not the one... What I'm trying to do is to call a function, but monitor all the local variable accesses within that function. Wha

Re: How to set custom locals for function call?

2020-09-01 Thread Terry Reedy
On 9/1/2020 5:58 PM, Andras Tantos wrote: On 9/1/2020 12:41 PM, MRAB wrote: CPython is able to identify all of the local names of a function and, Note >>>of a function<<<. basically, for reasons of efficiency, it uses slots for the local names instead of an actual dict. 'locals()' just re

Re: How to set custom locals for function call?

2020-09-01 Thread Chris Angelico
On Wed, Sep 2, 2020 at 8:44 AM Andras Tantos wrote: > > While I'm sure you're right, it certainly is well hidden: > > Python 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, > 07:34:03) [MSC v.1916 64 bit (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more informa

Re: How to set custom locals for function call?

2020-09-01 Thread Andras Tantos
On 9/1/2020 12:41 PM, MRAB wrote: On 2020-09-01 05:45, Andras Tantos wrote: All, I'm new here, so please direct me to the right forum, if this is not the one... What I'm trying to do is to call a function, but monitor all the local variable accesses within that function. What I thought I woul

Re: How to set custom locals for function call?

2020-09-01 Thread Chris Angelico
On Wed, Sep 2, 2020 at 5:00 AM Andras Tantos wrote: > > All, > > I'm new here, so please direct me to the right forum, if this is not the > one... > > What I'm trying to do is to call a function, but monitor all the local > variable accesses within that function. What I thought I would need to > d

Re: How to set custom locals for function call?

2020-09-01 Thread MRAB
On 2020-09-01 05:45, Andras Tantos wrote: All, I'm new here, so please direct me to the right forum, if this is not the one... What I'm trying to do is to call a function, but monitor all the local variable accesses within that function. What I thought I would need to do, is to |exec| the funct

How to set custom locals for function call?

2020-09-01 Thread Andras Tantos
All, I'm new here, so please direct me to the right forum, if this is not the one... What I'm trying to do is to call a function, but monitor all the local variable accesses within that function. What I thought I would need to do, is to |exec| the function with a custom |locals| dictionary.

Re: write function call _io_BufferedWriter_write_impl twice?

2019-05-30 Thread Inada Naoki
The second _io_BufferedWriter_write_impl call is for show warning like "ResourceWarning: unclosed file <_io.BufferedRandom name='myfile'>" Try `f.close()`, or `with open(...)`. On Thu, May 30, 2019 at 12:03 PM Windson Yang wrote: > > My script looks like this: > > f = open('myfile', 'a+b') >

write function call _io_BufferedWriter_write_impl twice?

2019-05-29 Thread Windson Yang
My script looks like this: f = open('myfile', 'a+b') f.write(b'abcde') And I also add a `printf` statement in the _io_BufferedWriter_write_impl function. static PyObject *

Re: function call questions

2016-10-23 Thread chenyong20000
在 2016年10月22日星期六 UTC+8下午9:15:06,Frank Millman写道: > wrote in message > news:9c91a4cf-1f3e-43b3-b75c-afc96b0b4...@googlegroups.com... > > > I have read Anssi's post already before I sent the post. To be frankly, I > can't understand why he got the right answer. I'm sorry for my silly. "So > when we

Re: function call questions

2016-10-22 Thread Frank Millman
wrote in message news:9c91a4cf-1f3e-43b3-b75c-afc96b0b4...@googlegroups.com... I have read Anssi's post already before I sent the post. To be frankly, I can't understand why he got the right answer. I'm sorry for my silly. "So when we assign to r again, it's the empty dict inside t (the one acc

Re: function call questions

2016-10-22 Thread chenyong20000
在 2016年10月22日星期六 UTC+8下午5:06:22,Frank Millman写道: > wrote in message > news:2853d778-857e-46fc-96a0-8d164c098...@googlegroups.com... > > 在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道: > > wrote in message > > news:01cfd810-0561-40b1-a834-95a73dad6...@googlegroups.com... > > > > Hi Frank, > > >

Re: function call questions

2016-10-22 Thread Frank Millman
wrote in message news:2853d778-857e-46fc-96a0-8d164c098...@googlegroups.com... 在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道: wrote in message news:01cfd810-0561-40b1-a834-95a73dad6...@googlegroups.com... Hi Frank, thanks for your kind help. What confused me is at this line: >>> r = r.set

Re: function call questions

2016-10-22 Thread chenyong20000
在 2016年10月20日星期四 UTC+8下午11:04:38,Frank Millman写道: > wrote in message > news:01cfd810-0561-40b1-a834-95a73dad6...@googlegroups.com... > > 在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道: > > wrote in message > > news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com... > > > > > Let's see if I

Re: function call questions

2016-10-21 Thread Frank Millman
"Anssi Saari" wrote in message news:vg3wph2i3dt@coffee.modeemi.fi... OK, so what happens is that now t references the dictionary with {'a': {}} and r references the empty dict inside that. So when we assign to r again, it's the empty dict inside t (the one accessed by key 'a') that change

Re: function call questions

2016-10-21 Thread Anssi Saari
"Frank Millman" writes: > Let's see if I can explain. I am using 't' and 'r' instead of 'tree' > and 'root', but otherwise it is the same as your original example. > t = {} r = t id(t) > 2542235910088 id(r) > 2542235910088 > > At this point, t and r are both references to the

Re: function call questions

2016-10-20 Thread Frank Millman
wrote in message news:01cfd810-0561-40b1-a834-95a73dad6...@googlegroups.com... 在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道: wrote in message news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com... > Let's see if I can explain. I am using 't' and 'r' instead of 'tree' and > 'root', bu

Re: function call questions

2016-10-20 Thread chenyong20000
在 2016年10月20日星期四 UTC+8下午1:32:18,Frank Millman写道: > wrote in message > news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com... > > 在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道: > > chenyong20...@gmail.com wrote: > > > > > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道: > > >> On 2016-10-19 03:15,

Re: function call questions

2016-10-19 Thread Frank Millman
wrote in message news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com... 在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道: chenyong20...@gmail.com wrote: > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道: >> On 2016-10-19 03:15, chenyong20...@gmail.com wrote: >> > Thanks Peter and Anssi for your ki

Re: function call questions

2016-10-19 Thread chenyong20000
在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道: > chenyong20...@gmail.com wrote: > > > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道: > >> On 2016-10-19 03:15, chenyong20...@gmail.com wrote: > >> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first > >> > question. But the second questi

Re: function call questions

2016-10-19 Thread Peter Otten
chenyong20...@gmail.com wrote: > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道: >> On 2016-10-19 03:15, chenyong20...@gmail.com wrote: >> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first >> > question. But the second question still confused me. Why "it seems that >> > after root =

Re: function call questions

2016-10-18 Thread chenyong20000
在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道: > On 2016-10-19 03:15, chenyong20...@gmail.com wrote: > > Thanks Peter and Anssi for your kind help. Now I'm ok with the first > > question. But the second question still confused me. Why "it seems that > > after root = root.setdefault(ch,{}) tree['a'] and

Re: function call questions

2016-10-18 Thread MRAB
On 2016-10-19 03:15, chenyong20...@gmail.com wrote: Thanks Peter and Anssi for your kind help. Now I'm ok with the first question. But the second question still confused me. Why "it seems that after root = root.setdefault(ch,{}) tree['a'] and root are the same object" and follows tree['a']['b']?

Re: function call questions

2016-10-18 Thread chenyong20000
Thanks Peter and Anssi for your kind help. Now I'm ok with the first question. But the second question still confused me. Why "it seems that after root = root.setdefault(ch,{}) tree['a'] and root are the same object" and follows tree['a']['b']? Thanks. -- https://mail.python.org/mailman/listinf

Re: function call questions

2016-10-18 Thread Peter Otten
chenyong20...@gmail.com wrote: >> > My question is: >> > (1) why root is always {}? >> >> Because that's what you bind it to in the line >> >> > ... root = root.setdefault(ch,{}) >> >> See >> for a description of the the s

Re: function call questions

2016-10-18 Thread chenyong20000
> > My question is: > > (1) why root is always {}? > > Because that's what you bind it to in the line > > > ... root = root.setdefault(ch,{}) > > See > for a description of the the setdefault() method. I'm still confused h

Re: function call questions

2016-10-18 Thread Anssi Saari
chenyong20...@gmail.com writes: > My question is: > (1) why root is always {}? Because that's what you wrote. root.setdefault(ch, {}) returns {} and you assign that to root. You probably want to do just root.setdefault(ch, {}) instead of root = root.setdefault(ch, {}). > (2) why tree is {'a': {'

Re: function call questions

2016-10-18 Thread Peter Otten
chenyong20...@gmail.com wrote: > Hi, > I got a function call as this: > >>>>def add_to_tree(root,value_string): > ...print "root is %s, value_string is %s" % (root, value_string) > ...for ch in value_string: > ... print "ch is

function call questions

2016-10-18 Thread chenyong20000
Hi, I got a function call as this: >>>def add_to_tree(root,value_string): ...print "root is %s, value_string is %s" % (root, value_string) ...for ch in value_string: ... print "ch is %s" % ch ... root = root.setdefault(ch,{}) ... print "r

Re: Supply condition in function call

2015-03-29 Thread Manuel Graune
Cameron Simpson writes: > > In xterm and I think several other X11 terminals, Shift-Insert > pastes. I found that _way_ more convenient than middle click. The > mouse is not your friend. > This information might prove as useful as your answer to my original question. ;-) Thanks for both! -- A

Re: Supply condition in function call

2015-03-29 Thread Cameron Simpson
On 28Mar2015 20:57, Steven D'Aprano wrote: On Sat, 28 Mar 2015 08:19 pm, Ian Kelly wrote: I've never been a fan of the primary selection style anyway. Copying text is conceptually an action. Selecting text is how one indicates the target of an action; conceptually it is not an action itself an

Re: Supply condition in function call

2015-03-28 Thread Steven D'Aprano
On Sat, 28 Mar 2015 08:19 pm, Ian Kelly wrote: > I've never been a fan of the primary selection style anyway. Copying > text is conceptually an action. Selecting text is how one indicates > the target of an action; conceptually it is not an action itself and > shouldn't cause an action to be perfo

Re: Supply condition in function call

2015-03-28 Thread Steven D'Aprano
On Sat, 28 Mar 2015 06:50 pm, Marko Rauhamaa wrote: > Larry Hudson : > >> Highlight the selection you want copied, move the mouse cursor to the >> location you want it copied to and middle-click with the mouse. Works >> between programs as well as within a single program. And it copies >> directl

Re: Supply condition in function call

2015-03-28 Thread Ian Kelly
On Sat, Mar 28, 2015 at 1:50 AM, Marko Rauhamaa wrote: > Larry Hudson : > >> Highlight the selection you want copied, move the mouse cursor to the >> location you want it copied to and middle-click with the mouse. Works >> between programs as well as within a single program. And it copies >> direc

Re: Supply condition in function call

2015-03-28 Thread Marko Rauhamaa
Larry Hudson : > Highlight the selection you want copied, move the mouse cursor to the > location you want it copied to and middle-click with the mouse. Works > between programs as well as within a single program. And it copies > directly without going through the clipboard. Unfortunately, Linux

Re: Supply condition in function call

2015-03-28 Thread Chris Angelico
On Sat, Mar 28, 2015 at 6:32 PM, Steven D'Aprano wrote: >> | Methods defined here: > > This is the usual guff that help() prints when you pass it a class or type. > Occasionally it is useful. Often it is not, especially the dunder methods. > > Unfortunately help's UI is rather primitive. It woul

Re: Supply condition in function call

2015-03-28 Thread Steven D'Aprano
By the way, you're not alone in recognising that Python 3 may be a little harder to teach to beginners than Python 2. Raymond Hettinger, one of the most respected Pythonistas around, has pointed out the same thing. More below. On Sat, 28 Mar 2015 12:48 am, Rustom Mody wrote: > On Friday, March

Re: Supply condition in function call

2015-03-27 Thread Rustom Mody
On Saturday, March 28, 2015 at 5:57:08 AM UTC+5:30, Larry Hudson wrote: > On 03/26/2015 06:56 PM, Chris Angelico wrote: > > On Fri, Mar 27, 2015 at 12:41 PM, Rustom Mody wrote: > [snip] > >> After selecting the line above [inside python inside help(filter) ]for > >> cut-pasting here, by mistake I p

Re: Supply condition in function call

2015-03-27 Thread Larry Hudson
On 03/26/2015 06:56 PM, Chris Angelico wrote: On Fri, Mar 27, 2015 at 12:41 PM, Rustom Mody wrote: [snip] After selecting the line above [inside python inside help(filter) ]for cut-pasting here, by mistake I pressed Ctrl-C rather than Ctrl-Shift-C An exception was thrown and the terminal remai

Re: Supply condition in function call

2015-03-27 Thread Cameron Simpson
On 27Mar2015 21:02, Manuel Graune wrote: Cameron Simpson writes: This passes the local variables inside test1() to "condition" as a single parameter. Now, I grant that vars['i'] is a miracle of tediousness. So consider this elaboration: from collections import namedtuple condition_test =

Re: Supply condition in function call

2015-03-27 Thread Peter Otten
Manuel Graune wrote: > Peter Otten <__pete...@web.de> writes: > >> Cameron Simpson wrote: >> >>> test1([0,1,2,3], [1,2,3,4], condition_test) >>> >>> This passes the local variables inside test1() to "condition" as a >>> single parameter. Now, I grant that vars['i'] is a miracle of >>> tediousn

Re: Supply condition in function call

2015-03-27 Thread Manuel Graune
Cameron Simpson writes: > This passes the local variables inside test1() to "condition" as a > single parameter. Now, I grant that vars['i'] is a miracle of > tediousness. So consider this elaboration: > > from collections import namedtuple > > condition_test = lambda vars: vars.i + vars.j > 4

Re: Supply condition in function call

2015-03-27 Thread Manuel Graune
Peter Otten <__pete...@web.de> writes: > Cameron Simpson wrote: > >> test1([0,1,2,3], [1,2,3,4], condition_test) >> >> This passes the local variables inside test1() to "condition" as a single >> parameter. Now, I grant that vars['i'] is a miracle of tediousness. So >> consider this elaboration

Re: Supply condition in function call

2015-03-27 Thread Mark Lawrence
On 27/03/2015 13:48, Rustom Mody wrote: On Friday, March 27, 2015 at 10:05:21 AM UTC+5:30, Steven D'Aprano wrote: On Fri, 27 Mar 2015 01:21 pm, Rustom Mody wrote: Anyway my point is that in python (after 2.2??) saying something is an object is a bit of a tautology -- ie verbiage without inform

Re: Supply condition in function call

2015-03-27 Thread Chris Angelico
On Sat, Mar 28, 2015 at 12:48 AM, Rustom Mody wrote: > Knows some C, not much else. > Starts studying python. > Good until a point. > Then suddenly hit... map, filter, and the worst of all lambda. > More he reads less he understands. > Tries help... Gets the above. > > So which do you think helps

Re: Supply condition in function call

2015-03-27 Thread Rustom Mody
On Friday, March 27, 2015 at 10:05:21 AM UTC+5:30, Steven D'Aprano wrote: > On Fri, 27 Mar 2015 01:21 pm, Rustom Mody wrote: > > > Anyway my point is that in python (after 2.2??) saying something is an > > object is a bit of a tautology -- ie verbiage without information. > > > Er, it's *always*

Re: Supply condition in function call

2015-03-27 Thread Dave Angel
On 03/26/2015 09:41 PM, Rustom Mody wrote: On Thursday, March 26, 2015 at 11:30:57 AM UTC+5:30, Chris Angelico wrote: Python 3.5.0a0 (default:4709290253e3, Jan 20 2015, 21:48:07) [GCC 4.7.2] on linux class filter(object) | filter(function or None, iterable) --> filter object | | Retur

Re: Supply condition in function call

2015-03-26 Thread Steven D'Aprano
On Fri, 27 Mar 2015 01:21 pm, Rustom Mody wrote: > Anyway my point is that in python (after 2.2??) saying something is an > object is a bit of a tautology -- ie verbiage without information. Er, it's *always* been a tautology. Every value in Python is an object, including classes, and that has b

Re: Supply condition in function call

2015-03-26 Thread Rustom Mody
On Friday, March 27, 2015 at 7:56:16 AM UTC+5:30, Ian wrote: > On Thu, Mar 26, 2015 at 7:56 PM, Chris Angelico wrote: > >> On a more specific note, its the 1st line: > >> > >> class filter(object) > >> > >> which knocks me off. > >> If a more restricted type from the ABC was shown which exactly ca

Re: Supply condition in function call

2015-03-26 Thread Rustom Mody
On Friday, March 27, 2015 at 7:26:54 AM UTC+5:30, Chris Angelico wrote: > On Fri, Mar 27, 2015 at 12:41 PM, Rustom Mody wrote: > > On a more specific note, its the 1st line: > > > > class filter(object) > > > > which knocks me off. > > If a more restricted type from the ABC was shown which exactly

Re: Supply condition in function call

2015-03-26 Thread Ian Kelly
On Thu, Mar 26, 2015 at 7:56 PM, Chris Angelico wrote: >> On a more specific note, its the 1st line: >> >> class filter(object) >> >> which knocks me off. >> If a more restricted type from the ABC was shown which exactly captures all >> the iterator-specific stuff like __iter__, __next__ it would

Re: Supply condition in function call

2015-03-26 Thread Chris Angelico
On Fri, Mar 27, 2015 at 12:41 PM, Rustom Mody wrote: > On Thursday, March 26, 2015 at 11:30:57 AM UTC+5:30, Chris Angelico wrote: >> On Thu, Mar 26, 2015 at 3:02 PM, Rustom Mody wrote: >> > [And BTW >> > help(filter) in python2 is much better documention than in python3 >> > ] >> >> Python 2.7.3

Re: Supply condition in function call

2015-03-26 Thread Rustom Mody
On Thursday, March 26, 2015 at 11:30:57 AM UTC+5:30, Chris Angelico wrote: > On Thu, Mar 26, 2015 at 3:02 PM, Rustom Mody wrote: > > [And BTW > > help(filter) in python2 is much better documention than in python3 > > ] > > Python 2.7.3 (default, Mar 13 2014, 11:03:55) > [GCC 4.7.2] on linux2 > >

Re: Supply condition in function call

2015-03-26 Thread Cameron Simpson
On 26Mar2015 11:37, Peter Otten <__pete...@web.de> wrote: You are right. [...] By the way, in this case you don't need the list at all: def vartuple(vars): return namedtuple("locals", vars)._make(vars.values()) Hmm. Neat. I had not realised that was available. You'd need "vars.keys()", no

Re: Supply condition in function call

2015-03-26 Thread Grant Edwards
On 2015-03-25, Ian Kelly wrote: > On Wed, Mar 25, 2015 at 1:53 PM, Grant Edwards > wrote: >> On 2015-03-25, Ian Kelly wrote: >>> On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune >>> wrote: >>> I'm looking for a way to supply a condition to an if-statement inside a function body when

Re: Supply condition in function call

2015-03-26 Thread Peter Otten
Cameron Simpson wrote: > On 26Mar2015 10:03, Peter Otten <__pete...@web.de> wrote: >>Cameron Simpson wrote: >>> vars = locals() >>> varnames = list(vars.keys()) >> >>That leaves varnames in undefined order. Consider >> >>varnames = sorted(vars) > > Actually, not necessary. > > I star

Re: Supply condition in function call

2015-03-26 Thread Cameron Simpson
On 26Mar2015 10:03, Peter Otten <__pete...@web.de> wrote: Cameron Simpson wrote: vars = locals() varnames = list(vars.keys()) That leaves varnames in undefined order. Consider varnames = sorted(vars) Actually, not necessary. I started with sorted, but it is irrelevant, so I bac

Re: Supply condition in function call

2015-03-26 Thread Peter Otten
Cameron Simpson wrote: > On 26Mar2015 07:27, Manuel Graune wrote: >>Gary Herron writes: >>> On 03/25/2015 10:29 AM, Manuel Graune wrote: def test1(a, b, condition="True"): for i,j in zip(a,b): c=i+j if eval(condition): print("Foo")

Re: Supply condition in function call

2015-03-26 Thread Cameron Simpson
On 26Mar2015 07:27, Manuel Graune wrote: Gary Herron writes: On 03/25/2015 10:29 AM, Manuel Graune wrote: def test1(a, b, condition="True"): for i,j in zip(a,b): c=i+j if eval(condition): print("Foo") test1([0,1,2,3],[1,2,3,4],"i+j >4") print("Bar") test1([

Re: Supply condition in function call

2015-03-25 Thread Manuel Graune
Gary Herron writes: > On 03/25/2015 10:29 AM, Manuel Graune wrote: >> >> def test1(a, b, condition="True"): >> for i,j in zip(a,b): >> c=i+j >> if eval(condition): >> print("Foo") >> >> test1([0,1,2,3],[1,2,3,4],"i+j >4") >> print("Bar") >> test1([0,1,2,3],[1,2,

Re: Supply condition in function call

2015-03-25 Thread Chris Angelico
On Thu, Mar 26, 2015 at 3:02 PM, Rustom Mody wrote: > [And BTW > help(filter) in python2 is much better documention than in python3 > ] Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2 filter(...) filter(function or None, sequence) -> list, tuple, or string Return tho

Re: Supply condition in function call

2015-03-25 Thread Rustom Mody
On Thursday, March 26, 2015 at 12:44:03 AM UTC+5:30, Gary Herron wrote: > On 03/25/2015 10:29 AM, Manuel Graune wrote: > > Hi, > > > > I'm looking for a way to supply a condition to an if-statement inside a > > function body when calling the function. I can sort of get what I want > > with using ev

Re: Supply condition in function call

2015-03-25 Thread Ian Kelly
On Wed, Mar 25, 2015 at 1:53 PM, Grant Edwards wrote: > On 2015-03-25, Ian Kelly wrote: >> On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune >> wrote: >> >>> I'm looking for a way to supply a condition to an if-statement inside a >>> function body when calling the function. I can sort of get what

Re: Supply condition in function call

2015-03-25 Thread Grant Edwards
On 2015-03-25, Ian Kelly wrote: > On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune > wrote: > >> I'm looking for a way to supply a condition to an if-statement inside a >> function body when calling the function. I can sort of get what I want >> with using eval [...] > > Pass the condition as a f

Re: Supply condition in function call

2015-03-25 Thread Gary Herron
On 03/25/2015 10:29 AM, Manuel Graune wrote: Hi, I'm looking for a way to supply a condition to an if-statement inside a function body when calling the function. I can sort of get what I want with using eval (see code below) but I would like to achieve this in a safer way. If there is a solution

Re: Supply condition in function call

2015-03-25 Thread Terry Reedy
On 3/25/2015 1:29 PM, Manuel Graune wrote: Hi, I'm looking for a way to supply a condition to an if-statement inside a function body when calling the function. I can sort of get what I want with using eval (see code below) but I would like to achieve this in a safer way. If there is a solution w

Re: Supply condition in function call

2015-03-25 Thread Joel Goldstick
On Wed, Mar 25, 2015 at 2:22 PM, Ian Kelly wrote: > On Wed, Mar 25, 2015 at 12:17 PM, Joel Goldstick > wrote: >> Oh, now I see. Do you know about this: >> https://docs.python.org/2/library/ast.html#ast.literal_eval > > As the name suggests, that only evals literals. It won't work for > complex e

Re: Supply condition in function call

2015-03-25 Thread Ian Kelly
On Wed, Mar 25, 2015 at 12:17 PM, Joel Goldstick wrote: > Oh, now I see. Do you know about this: > https://docs.python.org/2/library/ast.html#ast.literal_eval As the name suggests, that only evals literals. It won't work for complex expressions like "i + j > 4" -- https://mail.python.org/mailma

Re: Supply condition in function call

2015-03-25 Thread Joel Goldstick
On Wed, Mar 25, 2015 at 1:51 PM, Manuel Graune wrote: > Joel Goldstick writes: > >> On Wed, Mar 25, 2015 at 1:29 PM, Manuel Graune >> wrote: >>> >>> def test1(a, b, condition="True"): >>> for i,j in zip(a,b): >>> c=i+j >>> if eval(condition): >>>print("Foo") >>>

Re: Supply condition in function call

2015-03-25 Thread Manuel Graune
Joel Goldstick writes: > On Wed, Mar 25, 2015 at 1:29 PM, Manuel Graune wrote: >> >> def test1(a, b, condition="True"): >> for i,j in zip(a,b): >> c=i+j >> if eval(condition): >>print("Foo") >> > I'm not sure I understand your question, but condition will evaluate

Re: Supply condition in function call

2015-03-25 Thread Ian Kelly
On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune wrote: > Hi, > > I'm looking for a way to supply a condition to an if-statement inside a > function body when calling the function. I can sort of get what I want > with using eval (see code below) but I would like to achieve this in a > safer way. If

Re: Supply condition in function call

2015-03-25 Thread Joel Goldstick
On Wed, Mar 25, 2015 at 1:29 PM, Manuel Graune wrote: > Hi, > > I'm looking for a way to supply a condition to an if-statement inside a > function body when calling the function. I can sort of get what I want > with using eval (see code below) but I would like to achieve this in a > safer way. If

Supply condition in function call

2015-03-25 Thread Manuel Graune
Hi, I'm looking for a way to supply a condition to an if-statement inside a function body when calling the function. I can sort of get what I want with using eval (see code below) but I would like to achieve this in a safer way. If there is a solution which is safer while being less flexible, that

Re: Uniform Function Call Syntax (UFCS)

2014-06-09 Thread jongiddy
So, just to summarise the discussion: There was some very mild support for readable pipelines, either using UFCS or an alternative syntax, but the Pythonic way to make combinations of function and method applications readable is to assign to variables over multiple lines. Make the code read do

Re: Uniform Function Call Syntax (UFCS)

2014-06-09 Thread Ian Kelly
On Jun 8, 2014 9:56 PM, "Steven D'Aprano" > > which means that hasattr (which is defined by > > attempting to get the attribute and seeing if an exception is thrown) > > has to return True. > > Yes. And this is a problem why? Earlier in this thread I pointed out that returning True creates problem

Re: Uniform Function Call Syntax (UFCS)

2014-06-09 Thread Steven D'Aprano
On Mon, 09 Jun 2014 19:13:40 +1000, Chris Angelico wrote: > On Mon, Jun 9, 2014 at 7:09 PM, Steven D'Aprano > wrote: >> On Mon, 09 Jun 2014 09:25:33 +0300, Marko Rauhamaa wrote: >> >>> In a word, >>> Python has predefined a handful of *generic functions/methods*, >> >> >> That's nine words :-) >

Re: Uniform Function Call Syntax (UFCS)

2014-06-09 Thread Chris Angelico
On Mon, Jun 9, 2014 at 7:09 PM, Steven D'Aprano wrote: > On Mon, 09 Jun 2014 09:25:33 +0300, Marko Rauhamaa wrote: > >> In a word, >> Python has predefined a handful of *generic functions/methods*, > > > That's nine words :-) I'll explain in two words: We propose to marry your daughters. http://

Re: Uniform Function Call Syntax (UFCS)

2014-06-09 Thread Steven D'Aprano
On Mon, 09 Jun 2014 09:25:33 +0300, Marko Rauhamaa wrote: > In a word, > Python has predefined a handful of *generic functions/methods*, That's nine words :-) -- Steven D'Aprano http://import-that.dreamwidth.org/ -- https://mail.python.org/mailman/listinfo/python-list

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread jongiddy
On Monday, 9 June 2014 04:44:22 UTC+1, Chris Angelico wrote: > This could be solved, though, by having a completely different symbol > that means "the thing on my left is actually the first positional > parameter in the function call on my right", such as in your exampl

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Marko Rauhamaa
Steven D'Aprano : > On Sun, 08 Jun 2014 18:56:47 +0300, Marko Rauhamaa wrote: >> In fact, what's the point of having the duality? >>x < y <==> x.__lt__(y) > > [...] > > Consider x + y. What happens? > > #1 First, Python checks whether y is an instance of a *subclass* of x. If > so, y gets pri

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Chris Angelico
On Mon, Jun 9, 2014 at 1:53 PM, Steven D'Aprano wrote: >> which means that hasattr (which is defined by >> attempting to get the attribute and seeing if an exception is thrown) >> has to return True. > > Yes. And this is a problem why? > > Obviously it would mean that the semantics of hasattr will

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Steven D'Aprano
On Sun, 08 Jun 2014 18:56:47 +0300, Marko Rauhamaa wrote: > Paul Sokolovsky : > >> Python already has that - like, len(x) calls x.__len__() if it's >> defined > > In fact, what's the point of having the duality? > >len(x) <==> x.__len__() > >x < y <==> x.__lt__(y) > >str(x) <==> x.

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Steven D'Aprano
On Mon, 09 Jun 2014 02:48:13 +1000, Chris Angelico wrote: > class Circle: > def squared(self): > raise NotImplementedError("Proven impossible in 1882") > > The trouble is that logically Circle does have a 'squared' attribute, > while 3 doesn't; and yet Python guarantees this: > > foo

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Roy Smith
In article <53952807$0$29988$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > (Note that Forth is brilliant here, as it exposes the argument stack and > gives you a rich set of stack manipulation commands.) As does PostScript (which, despite its reputation as a printer format, is

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Chris Angelico
eeded it). This is a bad idea in Pike for the same reason it's a bad idea in Python - you end up with a massively polluted global namespace. This could be solved, though, by having a completely different symbol that means "the thing on my left is actually the first positional parameter in the

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Steven D'Aprano
On Mon, 09 Jun 2014 03:10:03 +1000, Chris Angelico wrote: [...] > Actually, this is something that I've run into sometimes. I can't think > of any Python examples, partly because Python tends to avoid unnecessary > method chaining, but the notion of "data flow" is a very clean one - > look at shell

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread jongiddy
On Sunday, 8 June 2014 18:24:28 UTC+1, Ian wrote: > > But that would all be done in getattr, so I don't think it affects > hasattr's implementation at all. Since hasattr doesn't push anything > onto the stack, getattr doesn't have to care whether it was called > directly from Python or indirectl

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Ian Kelly
On Sun, Jun 8, 2014 at 11:13 AM, Chris Angelico wrote: > On Mon, Jun 9, 2014 at 3:08 AM, Ian Kelly wrote: >> On Sun, Jun 8, 2014 at 10:48 AM, Chris Angelico wrote: >>> Except that it's even more complicated than that, because hasattr >>> wasn't defined in your module, so it has a different set o

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Chris Angelico
On Mon, Jun 9, 2014 at 3:08 AM, Ian Kelly wrote: > On Sun, Jun 8, 2014 at 10:48 AM, Chris Angelico wrote: >> Except that it's even more complicated than that, because hasattr >> wasn't defined in your module, so it has a different set of globals. >> In fact, this would mean that hasattr would bec

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Chris Angelico
On Mon, Jun 9, 2014 at 2:24 AM, jongiddy wrote: > A contrived example - which of these is easier to understand? > > from base64 import b64encode > > # works now > print(b64encode(str(min(map(int, f.readlines()), key=lambda n: n % 10)), > b'?-')) > > # would work with UFCS > f.readlines().map(int)

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Ian Kelly
On Sun, Jun 8, 2014 at 10:48 AM, Chris Angelico wrote: > Except that it's even more complicated than that, because hasattr > wasn't defined in your module, so it has a different set of globals. > In fact, this would mean that hasattr would become quite useless. hasattr is a builtin, so it has no

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Ian Kelly
On Sun, Jun 8, 2014 at 2:15 AM, jongiddy wrote: > One problem with your untested code, the superclasses would need to be > checked before using UFCS, so the structure is: > > try: > return super().__getattr__(attr) > except AttributeError: > # resolve using UFCS And then if UFCS finds no

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Ian Kelly
On Sun, Jun 8, 2014 at 10:24 AM, jongiddy wrote: > A contrived example - which of these is easier to understand? > > from base64 import b64encode > > # works now > print(b64encode(str(min(map(int, f.readlines()), key=lambda n: n % 10)), > b'?-')) > > # would work with UFCS > f.readlines().map(int

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Chris Angelico
On Mon, Jun 9, 2014 at 1:39 AM, jongiddy wrote: > e.g. I could define: > > def squared(x): > return x * x > > i = 3 > i.squared() => 9 > > j = AClassThatImplements__mul__() > j.squared() => whatever j * j returns > > but also: > class AnotherClass: > def __mul__(self, other): > ...

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Paul Sokolovsky
;re few generic operators - the fact that they are really generic (apply to wide different classes of objects) is exactly the reason why the're defined in global namespace, and not methods. And yep, I see things like "len" as essentially an operator, even though its name consists of let

Re: Uniform Function Call Syntax (UFCS)

2014-06-08 Thread Ian Kelly
On Sun, Jun 8, 2014 at 9:56 AM, Marko Rauhamaa wrote: > Paul Sokolovsky : > >> Python already has that - like, len(x) calls x.__len__() if it's >> defined > > In fact, what's the point of having the duality? > >len(x) <==> x.__len__() > >x < y <==> x.__lt__(y) > >str(x) <==> x.__str__(

  1   2   3   4   5   >