Re: How to set environmental variables for Python
On 17Jan2022 11:36, Shaozhong SHI wrote: >Set Operation System but not disturbing existing setting. Only to add at >the command line. If you mean: "set on the command line so that I run some script using Python 3.6.1", usually you would just invoke the specific Python 3.6.1 executable. You can do that directly, or modify $PATH (UNIX, %path% on Windows?) to find that executable first when looking for the "python" (or "python3" or "py") command, or use a virtual environment. The first approach (direct execution) might look like this: [~]fleet2*> /usr/local/bin/python3.10 Python 3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:20) [Clang 12.0.5 (clang-1205.0.22.11)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> That's on my local Mac, "[~]fleet2*>" is my prompt, and there's a Python 3.10 installed as /usr/local/bin/python3.10. The second approach might look like this: env PATH=/Library/Frameworks/Python.framework/Versions/3.10/bin:$PATH python That places the Python 3.10 "bin" directory in my $PATH _ahead_ of all other paths, so that "python" is found there first, thus running the desired python version: [~]fleet2*> env PATH=/Library/Frameworks/Python.framework/Versions/3.10/bin:$PATH python3 Python 3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:20) [Clang 12.0.5 (clang-1205.0.22.11)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> That particular long path is an artifact of how Python is installed on my Mac. Adjust for your platform. The third approach is to use a virtual environment, a common approach for python development. A virtual environment is a little install directory based on a particular python version, where you can install a custom set of third party modules. You make one like this: /usr/local/bin/python3.10 -m venv venv That uses the "venv" module from python 3.10 to create a new virtual environment in the directory "venv" (in the current directory). >From that point onward you want the virtual env "bin" directory in your PATH: export PATH=$PWD/venv/bin:$PATH and thereon, _in that shell_, "python3" will run the python from the virtual environment (which uses the python3.10 you used to create the venv) and "pip3" will install modules into that virtual environment, not disturbing other setups. Virtualenvs come with an "activate" script whose purpose it to set up your current shell to use the environment; they essentially do the "export" above and also fiddle your prompt to remind you that you're using a particular environment. You don't need to use that - technically it is enough to directly invoke the python3 executable from the environment. Fiddling $PATH lets other things find that "python3" by default. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How to set environmental variables for Python
Set Operation System but not disturbing existing setting. Only to add at the command line. Regards, David On Mon, 17 Jan 2022 at 10:57, dn via Python-list wrote: > On 17/01/2022 22.31, Shaozhong SHI wrote: > > I got quite a few version of Python on my machine. > > > > How do I set environmental variables for Python 3.6.1 to work? > > > Set from Python, or set in the OpSys? > > https://docs.python.org/3/library/os.html?highlight=environment%20variable > > MS-Win: https://docs.python.org/3/using/windows.html#setting-envvars > -- > Regards, > =dn > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How to set environmental variables for Python
On 17/01/2022 22.31, Shaozhong SHI wrote: > I got quite a few version of Python on my machine. > > How do I set environmental variables for Python 3.6.1 to work? Set from Python, or set in the OpSys? https://docs.python.org/3/library/os.html?highlight=environment%20variable MS-Win: https://docs.python.org/3/using/windows.html#setting-envvars -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
How to set environmental variables for Python
I got quite a few version of Python on my machine. How do I set environmental variables for Python 3.6.1 to work? Regards, David -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
On 16/06/21 10:51 pm, Elena wrote: sorry I wrote it wrongly, my bad, I will use f just to predict yi from new coming Xi. Then what do you do with the new yi? -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
Il Wed, 16 Jun 2021 11:37:42 +1200, Greg Ewing ha scritto: > On 15/06/21 10:07 pm, Elena wrote: >> After the optimization, I will use f just to predict new Xi. > > So you're going to use f backwards? > > I don't see how that will work. Where are you going to find a new yi to > feed into the inverse of f? > > I think I don't understand what role g plays in all of this. If the > ultimate goal is to find a better mixture, > you need some kind of figure of merit for an individual mixture. But you > don't have that, you only have this thing g that somehow depends on all > of your mixtures at once. > > I'm still not seeing the big picture. sorry I wrote it wrongly, my bad, I will use f just to predict yi from new coming Xi. -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
On 15/06/21 10:07 pm, Elena wrote: After the optimization, I will use f just to predict new Xi. So you're going to use f backwards? I don't see how that will work. Where are you going to find a new yi to feed into the inverse of f? I think I don't understand what role g plays in all of this. If the ultimate goal is to find a better mixture, you need some kind of figure of merit for an individual mixture. But you don't have that, you only have this thing g that somehow depends on all of your mixtures at once. I'm still not seeing the big picture. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
Il Tue, 15 Jun 2021 01:53:09 +, Martin Di Paola ha scritto: > From what I'm understanding it is an "optimization problem" like the > ones that you find in "linear programming". > > But in your case the variables are not Real (they are Integers) and the > function to minimize g() is not linear. > > You could try/explore CVXPY (https://www.cvxpy.org/) which it's a solver > for different kinds of "convex programming". I don't have experience > with it however. > > The other weapon in my arsenal would be Z3 > (https://theory.stanford.edu/~nikolaj/programmingz3.html) which it's a > SMT/SAT solver with a built-in extension for optimization problems. > > I've more experience with this so here is a "draft" of what you may be > looking for. > > > from z3 import Integers, Optimize, And, If > > # create a Python array X with 3 Z3 Integer variables named x0, x1, x2 X > = Integers('x0 x1 x2') > Y = Integers('y0 y1') > > # create the solver solver = Optimize() > > # add some restrictions like lower and upper bounds for x in X: >solver.add(And(0 <= x, x <= 2)) # each x is between 0 and 2 > for y in Y: >solver.add(And(0 <= y, y <= 2)) > > def f(X): ># Conditional expression can be modeled too with "If" ># These are *not* evaluated like a normal Python "if" but # modeled >as a whole. It'll be the solver which will "run it" >return If( > And(x[0] == 0, x[1] == 0), # the condition Y[0] == 0, # Y[0] will > *must* be 0 *if* the condition holds Y[0] == 2 # Y[0] will *must* > be 2 *if* the condition doesn't hold ) > > solver.add(f(X)) > > # let's define the function to optimize g = Y[0]**2 solver.maximize(g) > > # check if we have a solution solver.check() # this should return 'sat' > > # get one of the many optimum solutions solver.model() > > > I would recommend you to write a very tiny problem with 2 or 3 variables > and a very simple f() and g() functions, make it work (manually and with > Z3) and only then build a more complex program. > > You may find useful (or not) these two posts that I wrote a month ago > about Z3. These are not tutorials, just personal experience with a > concrete example. > > Combine Real, Integer and Bool variables: > https://book-of-gehn.github.io/articles/2021/05/02/Planning-Space- Missions.html > > Lookup Tables (this may be useful for programming a f() "variable" > function where the code of f() (the decision tree) is set by Z3 and not > by you such f() leads to the optimum of g()) > https://book-of-gehn.github.io/articles/2021/05/26/Casting-Broadcasting- LUT-and-Bitwise-Ops.html > > > Happy hacking. > Martin. > > Interesting, I completely didn't know about this Z3 tool, I'll try to go into that. Thank you for hint. BTW the first two links I think are broken. Ele -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
Il Tue, 15 Jun 2021 10:40:05 +1200, Greg Ewing ha scritto: > On 15/06/21 12:51 am, Elena wrote: > Hmmm, so the problem breaks down into two parts: > (1) find a vector Y that minimises g (2) find a set of rules that will > allow you to predict each component of Y from its corresponding X values > > Is that right? Correct, the split can be an interesting approach. > > I ztill don't really understand. What are you going to do with this > function f once you have it? > > I would have thought the idea was that if someone gives you a new > mixture X[n+1] you can use f to predict how well it will work. > But that will just give you a y[n+1], and it's not clear what to do with > that. Do you append it to Y and feed an n+1 component vector into g? > > I think I still need more information about the underlying problem > before I can help you much. After the optimization, I will use f just to predict new Xi. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
From what I'm understanding it is an "optimization problem" like the ones that you find in "linear programming". But in your case the variables are not Real (they are Integers) and the function to minimize g() is not linear. You could try/explore CVXPY (https://www.cvxpy.org/) which it's a solver for different kinds of "convex programming". I don't have experience with it however. The other weapon in my arsenal would be Z3 (https://theory.stanford.edu/~nikolaj/programmingz3.html) which it's a SMT/SAT solver with a built-in extension for optimization problems. I've more experience with this so here is a "draft" of what you may be looking for. from z3 import Integers, Optimize, And, If # create a Python array X with 3 Z3 Integer variables named x0, x1, x2 X = Integers('x0 x1 x2') Y = Integers('y0 y1') # create the solver solver = Optimize() # add some restrictions like lower and upper bounds for x in X: solver.add(And(0 <= x, x <= 2)) # each x is between 0 and 2 for y in Y: solver.add(And(0 <= y, y <= 2)) def f(X): # Conditional expression can be modeled too with "If" # These are *not* evaluated like a normal Python "if" but # modeled as a whole. It'll be the solver which will "run it" return If( And(x[0] == 0, x[1] == 0), # the condition Y[0] == 0, # Y[0] will *must* be 0 *if* the condition holds Y[0] == 2 # Y[0] will *must* be 2 *if* the condition doesn't hold ) solver.add(f(X)) # let's define the function to optimize g = Y[0]**2 solver.maximize(g) # check if we have a solution solver.check() # this should return 'sat' # get one of the many optimum solutions solver.model() I would recommend you to write a very tiny problem with 2 or 3 variables and a very simple f() and g() functions, make it work (manually and with Z3) and only then build a more complex program. You may find useful (or not) these two posts that I wrote a month ago about Z3. These are not tutorials, just personal experience with a concrete example. Combine Real, Integer and Bool variables: https://book-of-gehn.github.io/articles/2021/05/02/Planning-Space-Missions.html Lookup Tables (this may be useful for programming a f() "variable" function where the code of f() (the decision tree) is set by Z3 and not by you such f() leads to the optimum of g()) https://book-of-gehn.github.io/articles/2021/05/26/Casting-Broadcasting-LUT-and-Bitwise-Ops.html Happy hacking. Martin. On Mon, Jun 14, 2021 at 12:51:34PM +, Elena via Python-list wrote: Il Mon, 14 Jun 2021 19:39:17 +1200, Greg Ewing ha scritto: On 14/06/21 4:15 am, Elena wrote: Given a dataset of X={(x1... x10)} I can calculate Y=f(X) where f is this rule-based function. I know an operator g that can calculate a real value from Y: e = g(Y) g is too complex to be written analytically. I would like to find a set of rules f able to minimize e on X. There must be something missing from the problem description. From what you've said here, it seems like you could simply find a value k for Y that minimises g, regardless of X, and then f would consist of a single rule: y = k. Can you tell us in more concrete terms what X and g represent? I see what you mean, so I try to explain it better: Y is a vector say [y1, y2, ... yn], with large (n>>10), where yi = f(Xi) with Xi = [x1i, x2i, ... x10i] 1<=i<=n. All yi and xji assume discrete values. I already have a dataset of X={Xi} and would like to find the rules f able to minimize a complicated-undifferenciable Real function g(f(X)). Hope this makes more sense. x1...x10 are 10 chemical components that can be absent (0), present (1), modified (2). yi represent a quality index of the mixtures and g is a global quality of the whole process. Thank you in advance ele -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
On 15/06/21 12:51 am, Elena wrote: I see what you mean, so I try to explain it better: Y is a vector say [y1, y2, ... yn], with large (n>>10), where yi = f(Xi) with Xi = [x1i, x2i, ... x10i] 1<=i<=n. All yi and xji assume discrete values. I already have a dataset of X={Xi} and would like to find the rules f able to minimize a complicated-undifferenciable Real function g(f(X)). Hope this makes more sense. Hmmm, so the problem breaks down into two parts: (1) find a vector Y that minimises g (2) find a set of rules that will allow you to predict each component of Y from its corresponding X values Is that right? x1...x10 are 10 chemical components that can be absent (0), present (1), modified (2). yi represent a quality index of the mixtures and g is a global quality of the whole process. I ztill don't really understand. What are you going to do with this function f once you have it? I would have thought the idea was that if someone gives you a new mixture X[n+1] you can use f to predict how well it will work. But that will just give you a y[n+1], and it's not clear what to do with that. Do you append it to Y and feed an n+1 component vector into g? I think I still need more information about the underlying problem before I can help you much. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
On 6/13/21 12:15 PM, Elena via Python-list wrote: > Hi, I have, say 10 variables (x1 ... x10) which can assume discrete finite > values, for instance [0,1 or 2]. > I need to build a set of rules, such as: > > 1) if x1==0 and x2==1 and x10==2 then y = 1 > 2) if x2==1 and x3==1 and x4==2 and x6==0 then y = 0 > 3) if x2==0 and x3==1 then y = 2 > 4) if x6==0 and x7==2 then y = 0 > ... > ... > (actually it can be seen as a decision tree classifier). > > y can assume the same discrete value [0,1 or 2] > I don't know a-priori anything about the number of rules and the > combinations of the tested inputs. > > Given a dataset of X={(x1... x10)} I can calculate Y=f(X) where f is this > rule-based function. > > I know an operator g that can calculate a real value from Y: e = g(Y) > g is too complex to be written analytically. > > I would like to find a set of rules f able to minimize e on X. > > I know the problem can become NP-hard, but I would be fine also with a > suboptimal solution. > > What's the best way to approach the problem? > In case, does something already exist in python? > > > thank you My first feeling is that this doesn't have a lot of structure to do a lot of optimizations, and just brute forcing might be the answer. Of course, one option is just sort the rules by Y, then iterate through the rules and see if any data matches, for that putting the dataset into something like an SQLite database with indexes on the various X columns might work (with 10 columns, the 45 indexes on each column pair might make things reasonably efficient.) The biggest gain would happen if you could look at the rule set and find patterns in it. The big question is making sure that the rule set covers every value in the data array, and never gives one input value two different y values. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
Il Mon, 14 Jun 2021 19:39:17 +1200, Greg Ewing ha scritto: > On 14/06/21 4:15 am, Elena wrote: >> Given a dataset of X={(x1... x10)} I can calculate Y=f(X) where f is >> this rule-based function. >> >> I know an operator g that can calculate a real value from Y: e = g(Y) >> g is too complex to be written analytically. >> >> I would like to find a set of rules f able to minimize e on X. > > There must be something missing from the problem description. > From what you've said here, it seems like you could simply find > a value k for Y that minimises g, regardless of X, and then f would > consist of a single rule: y = k. > > Can you tell us in more concrete terms what X and g represent? I see what you mean, so I try to explain it better: Y is a vector say [y1, y2, ... yn], with large (n>>10), where yi = f(Xi) with Xi = [x1i, x2i, ... x10i] 1<=i<=n. All yi and xji assume discrete values. I already have a dataset of X={Xi} and would like to find the rules f able to minimize a complicated-undifferenciable Real function g(f(X)). Hope this makes more sense. x1...x10 are 10 chemical components that can be absent (0), present (1), modified (2). yi represent a quality index of the mixtures and g is a global quality of the whole process. Thank you in advance ele -- https://mail.python.org/mailman/listinfo/python-list
optimization of rule-based model on discrete variables
Hi, I have, say 10 variables (x1 ... x10) which can assume discrete finite values, for instance [0,1 or 2]. I need to build a set of rules, such as: 1) if x1==0 and x2==1 and x10==2 then y = 1 2) if x2==1 and x3==1 and x4==2 and x6==0 then y = 0 3) if x2==0 and x3==1 then y = 2 4) if x6==0 and x7==2 then y = 0 ... ... (actually it can be seen as a decision tree classifier). y can assume the same discrete value [0,1 or 2] I don't know a-priori anything about the number of rules and the combinations of the tested inputs. Given a dataset of X={(x1... x10)} I can calculate Y=f(X) where f is this rule-based function. I know an operator g that can calculate a real value from Y: e = g(Y) g is too complex to be written analytically. I would like to find a set of rules f able to minimize e on X. I know the problem can become NP-hard, but I would be fine also with a suboptimal solution. What's the best way to approach the problem? In case, does something already exist in python? thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: optimization of rule-based model on discrete variables
On 14/06/21 4:15 am, Elena wrote: Given a dataset of X={(x1... x10)} I can calculate Y=f(X) where f is this rule-based function. I know an operator g that can calculate a real value from Y: e = g(Y) g is too complex to be written analytically. I would like to find a set of rules f able to minimize e on X. There must be something missing from the problem description. From what you've said here, it seems like you could simply find a value k for Y that minimises g, regardless of X, and then f would consist of a single rule: y = k. Can you tell us in more concrete terms what X and g represent? -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: musings on static variables
r...@zedat.fu-berlin.de (Stefan Ram) writes: > This C program use a local /static/ variable. > > main.c > > #include > > int f( void ) > { static int i = 0; > return i++; } > > int main( void ) > { printf( "%d\n", f() ); > printf( "%d\n", f() ); > printf( "%d\n", f() ); } > > transcript > > 0 > 1 > 2 > > When asked how to do this in Python, sometimes people > suggest to use a module variable for this, or to add "i" > to "f" as an attribute, or maybe even to use some kind of > closure. Another (more pythonic?) way to do this, would be: > > main.py > > def f_(): > i = 0 > while True: > yield i > i += 1 > > f = f_() > print( next( f )) > print( next( f )) > print( next( f )) > > transcript > > 0 > 1 > 2 def f(i = [0]): i[0] += 1 return i[0] print(f()) print(f()) print(f()) maybe? More than a bit yucky, though. -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculations and Variables
On 01/11/2019 17:07, MRAB wrote: On 2019-11-01 12:36, Rhodri James wrote: On 31/10/2019 20:14, MRAB wrote: On 2019-10-31 18:46, ferzan saglam wrote: The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) Why are there parentheses around the inputs? Have you omitted the conversion to numbers? My guess is the OP is using Python2 rather than Python3. So my first piece of advice would be to switch to Python3 now so you don't have to re-learn oddities like input(), print() and string handling. The OP says it prints 44.44. In Python 2, / does integer division, so it would print 44.00. Well, it's inconsistent one way or the other. We'll have to wait for more information. -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculations and Variables
On 2019-11-01 12:36, Rhodri James wrote: On 31/10/2019 20:14, MRAB wrote: On 2019-10-31 18:46, ferzan saglam wrote: The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) Why are there parentheses around the inputs? Have you omitted the conversion to numbers? My guess is the OP is using Python2 rather than Python3. So my first piece of advice would be to switch to Python3 now so you don't have to re-learn oddities like input(), print() and string handling. The OP says it prints 44.44. In Python 2, / does integer division, so it would print 44.00. -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculations and Variables
On 31/10/2019 20:14, MRAB wrote: On 2019-10-31 18:46, ferzan saglam wrote: The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) Why are there parentheses around the inputs? Have you omitted the conversion to numbers? My guess is the OP is using Python2 rather than Python3. So my first piece of advice would be to switch to Python3 now so you don't have to re-learn oddities like input(), print() and string handling. -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculations and Variables
On 2019-10-31 18:46, ferzan saglam wrote: The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) Why are there parentheses around the inputs? Have you omitted the conversion to numbers? total = bill + (bill / tip) That adds a 1/9 of the bill. It should add 9% of the bill. eachPay = total / split print("Each person will have to pay %.2f" % eachPay) I am aiming for the result of 43.6, but somehow get the result of 44.44. (meal cost: 200) (Tip: 9) (people: 5) I seem to do the calculation below, but get different results each time. Total * Percentage Amount / 100 -- https://mail.python.org/mailman/listinfo/python-list
Re: Calculations and Variables
On 10/31/19 11:46 AM, ferzan...@gmail.com wrote: The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) total = bill + (bill / tip) Don't you mean total = bill + (bill * tip) ? eachPay = total / split print("Each person will have to pay %.2f" % eachPay) I am aiming for the result of 43.6, but somehow get the result of 44.44. (meal cost: 200) (Tip: 9) (people: 5) I seem to do the calculation below, but get different results each time. Total * Percentage Amount / 100 -- Dr. Gary Herron Professor of Computer Science DigiPen Institute of Technology (425) 895-4418 -- https://mail.python.org/mailman/listinfo/python-list
RE: Calculations and Variables
How are you getting any value at all? You are trying to do math on string values just like in your infinite loop question. This should raise TypeError: unsupported operand type(s) for /: 'str' and 'str' Also, tips are usually given as percentages. Here with a tip value of 9 you're saying that the tip is 1/9 th of the bill, which is where the number difference is coming from. If someone entered 50 this is saying the tip is 1/50 th of the bill, etc. -Original Message- From: Python-list On Behalf Of ferzan saglam Sent: Thursday, October 31, 2019 2:46 PM To: python-list@python.org Subject: Calculations and Variables The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) total = bill + (bill / tip) eachPay = total / split print("Each person will have to pay %.2f" % eachPay) I am aiming for the result of 43.6, but somehow get the result of 44.44. (meal cost: 200) (Tip: 9) (people: 5) I seem to do the calculation below, but get different results each time. Total * Percentage Amount / 100 -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Calculations and Variables
The code below which I have written should print the result of 43.6 with the given values I have included at the end of this question, but for some odd reason I get the result of 44.44. bill = (input("Enter the total cost of the meal: \n")) tip = (input("Enter how much the tip is: \n")) split = (input("Enter how many people there are: \n")) total = bill + (bill / tip) eachPay = total / split print("Each person will have to pay %.2f" % eachPay) I am aiming for the result of 43.6, but somehow get the result of 44.44. (meal cost: 200) (Tip: 9) (people: 5) I seem to do the calculation below, but get different results each time. Total * Percentage Amount / 100 -- https://mail.python.org/mailman/listinfo/python-list
Re: decorator needs access to variables where it is used.
09.10.19 14:02, Chris Angelico пише: The decorator has full access to the function object, including a reference to that function's module globals. def trace(func): log = func.__globals__["log"] ... proceed as before As long as you can depend on "log" always being a module-level (global) name, and not (for instance) a closure variable, this should work. It's a bit ugly, but it should be fine since it's buried away in the decorator. It may be better to not rely on global variable "log", but get the logger by the module name. def trace(func): log = logging.getLogger(func.__module__) ... -- https://mail.python.org/mailman/listinfo/python-list
Re: decorator needs access to variables where it is used.
On 9/10/19 13:02, Chris Angelico wrote: > On Wed, Oct 9, 2019 at 9:53 PM Antoon Pardon wrote: >> I have some logging utilities so that when I write library code, I just use >> the following. >> >> from logutil import Logger >> >> log = Logger(__name__) > Are you always absolutely consistent with this? Do you always have it > as a module-level variable and always called "log", and you never use > the name "log" for anything else? (Namespaced usage like "math.log" is > fine, but you never have a function-local "log" or anything.) I have been up to now, but I will take this under consideration when thinking about how to proceed. >> And from then on I just use log, to do the logging of that module. >> >> But now I would like to write a decorator trace, so that a decorated >> function would log, its arguments and result. >> >> Something like: >> >> def trace(func): >> >> def wrapper(*args): >> log(DEBUG, "=> %s%s" % (func.__name__, args)) >> result = func(*args) >> log(DEBUG, "%s => %s" (func.__name__, result)) >> >> return wrapper >> >> The problem is that with this decorater, the log function >> used, will be the log function defined where the decorator >> is written and I would prefer it to use the log function >> where it is used, so that it uses the same log function >> as the function it decorates. >> > The decorator has full access to the function object, including a > reference to that function's module globals. > > def trace(func): > log = func.__globals__["log"] > ... proceed as before > > As long as you can depend on "log" always being a module-level > (global) name, and not (for instance) a closure variable, this should > work. It's a bit ugly, but it should be fine since it's buried away in > the decorator. Nice idea, I can work with that. Antoon. -- https://mail.python.org/mailman/listinfo/python-list
Re: decorator needs access to variables where it is used.
Antoon Pardon wrote: > I have some logging utilities so that when I write library code, I just > use the following. > > from logutil import Logger > > log = Logger(__name__) If logutil is under your control you can make log a callable object with a tracing method: [logutil.py] class Logger: def __call__(self, ...): # what log() currently does def traced(self, func): def trace(*args): self(DEBUG, "=> %s%s" % (func.__name__, args)) result = func(*args) self(DEBUG, "%s => %s" (func.__name__, result)) return result return trace [useit.py] from logutil import Logger log = Logger(__name__) @log.traced def some_func(...): ... But yes, Chris' suggestion is more straight-forward ;) > And from then on I just use log, to do the logging of that module. > > But now I would like to write a decorator trace, so that a decorated > function would log, its arguments and result. > > Something like: > > def trace(func): > > def wrapper(*args): > log(DEBUG, "=> %s%s" % (func.__name__, args)) > result = func(*args) > log(DEBUG, "%s => %s" (func.__name__, result)) > > return wrapper > > The problem is that with this decorater, the log function > used, will be the log function defined where the decorator > is written and I would prefer it to use the log function > where it is used, so that it uses the same log function > as the function it decorates. -- https://mail.python.org/mailman/listinfo/python-list
Re: decorator needs access to variables where it is used.
On Wed, Oct 9, 2019 at 9:53 PM Antoon Pardon wrote: > > I have some logging utilities so that when I write library code, I just use > the following. > > from logutil import Logger > > log = Logger(__name__) Are you always absolutely consistent with this? Do you always have it as a module-level variable and always called "log", and you never use the name "log" for anything else? (Namespaced usage like "math.log" is fine, but you never have a function-local "log" or anything.) > And from then on I just use log, to do the logging of that module. > > But now I would like to write a decorator trace, so that a decorated > function would log, its arguments and result. > > Something like: > > def trace(func): > > def wrapper(*args): > log(DEBUG, "=> %s%s" % (func.__name__, args)) > result = func(*args) > log(DEBUG, "%s => %s" (func.__name__, result)) > > return wrapper > > The problem is that with this decorater, the log function > used, will be the log function defined where the decorator > is written and I would prefer it to use the log function > where it is used, so that it uses the same log function > as the function it decorates. > The decorator has full access to the function object, including a reference to that function's module globals. def trace(func): log = func.__globals__["log"] ... proceed as before As long as you can depend on "log" always being a module-level (global) name, and not (for instance) a closure variable, this should work. It's a bit ugly, but it should be fine since it's buried away in the decorator. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
decorator needs access to variables where it is used.
I have some logging utilities so that when I write library code, I just use the following. from logutil import Logger log = Logger(__name__) And from then on I just use log, to do the logging of that module. But now I would like to write a decorator trace, so that a decorated function would log, its arguments and result. Something like: def trace(func): def wrapper(*args): log(DEBUG, "=> %s%s" % (func.__name__, args)) result = func(*args) log(DEBUG, "%s => %s" (func.__name__, result)) return wrapper The problem is that with this decorater, the log function used, will be the log function defined where the decorator is written and I would prefer it to use the log function where it is used, so that it uses the same log function as the function it decorates. -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On Fri, Aug 23, 2019 at 4:00 AM Windson Yang wrote: > > Thank you all. I agreed with Frank that > > > It would make sense to use the 'global' keyword if you have a module > with various functions, several of which refer to 'foo', but only one of > which changes the value of 'foo'. > > I also found an example in cpython/lib/gettext.py, only 'textdomain > function' can change '_current_domain', other functions just refer to it. > So, it will be not evil or to use 'global' keyword correctly when there is > only one function can change its value? > > Cameron Simpson 于2019年8月23日周五 下午3:15写道: > The problem with deciding that only one function can change the value, is that someone else can change the other functions. So, if you are working alone, it won't be a problem, but once code with globals enters the 'wild' all bets are off. -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
Thank you all. I agreed with Frank that > It would make sense to use the 'global' keyword if you have a module with various functions, several of which refer to 'foo', but only one of which changes the value of 'foo'. I also found an example in cpython/lib/gettext.py, only 'textdomain function' can change '_current_domain', other functions just refer to it. So, it will be not evil or to use 'global' keyword correctly when there is only one function can change its value? Cameron Simpson 于2019年8月23日周五 下午3:15写道: > On 23Aug2019 09:07, Frank Millman wrote: > >On 2019-08-23 8:43 AM, Windson Yang wrote: > >>In class.py > >> > >> class Example: > >> def __init__(self): > >> self.foo = 1 > >> def bar() > >> return self.foo + 1 > >> > >>Expect the syntax, why using class variable self.foo would be better (or > >>more common)? I think the 'global' here is relative, foo is global in > >>global.py and self.foo is global in Example class. If the global.py is > >>short and clean enough (didn't have a lot of other class), they are > pretty > >>much the same. Or I missed something? > >> > > > >One difference is that you could have many instances of Example, each > >with its own value of 'foo', whereas with a global 'foo' there can > >only be one value of 'foo' for the module. > > But that is an _instance_ attribute. Which is actually what Windson Yang > made. > > A class attribute is bound to the class, not an instance. The > terminology is important. > > Cheers, > Cameron Simpson > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 23Aug2019 09:07, Frank Millman wrote: On 2019-08-23 8:43 AM, Windson Yang wrote: In class.py class Example: def __init__(self): self.foo = 1 def bar() return self.foo + 1 Expect the syntax, why using class variable self.foo would be better (or more common)? I think the 'global' here is relative, foo is global in global.py and self.foo is global in Example class. If the global.py is short and clean enough (didn't have a lot of other class), they are pretty much the same. Or I missed something? One difference is that you could have many instances of Example, each with its own value of 'foo', whereas with a global 'foo' there can only be one value of 'foo' for the module. But that is an _instance_ attribute. Which is actually what Windson Yang made. A class attribute is bound to the class, not an instance. The terminology is important. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 23Aug2019 14:43, Windson Yang wrote: I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class Example: def __init__(self): self.foo = 1 def bar() return self.foo + 1 This isn't a class variable. The .foo attribute will be attached to "self", which is an instance of the class, not the class itself. You probably want: class Example2: foo = 1 def __init__(self): pass So what's happening here? When the class is defined, in addition to defining the .__init__ method we also define .foo. These are attributes of the class: there's just one of each. In your code you're setting .foo on "self", so there's one per instance of the class. So in my Example2 there's a foo: print(Example2.foo) works. Compared to a global, this is nicely contained in the class' namespace. But otherwise it is the same: it gets defined just once as the module is imported (which defines the class Example2, which defines Example2.foo). However, it _is_ harder to use as a local by accident. Here: from example_module import Example2 z = 1 def f(): z = 3 Example2.foo = 3 Here, "z" is local to "f" because it is assigned to. Example2.foo is still the one from inside the class - we're not assigning to a function local name, but to a name _accessed_ via Example2. Expect the syntax, why using class variable self.foo would be better (or more common)? I think the 'global' here is relative, foo is global in global.py and self.foo is global in Example class. As mentioned, in your Example class self.foo is an attribute of the instance, not the class. My Example2 class shows how to put it on the class. If the global.py is short and clean enough (didn't have a lot of other class), they are pretty much the same. Or I missed something? Making a class level name has the nice aspect that it has a better conceptual context. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 2019-08-23 8:43 AM, Windson Yang wrote: I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class Example: def __init__(self): self.foo = 1 def bar() return self.foo + 1 Expect the syntax, why using class variable self.foo would be better (or more common)? I think the 'global' here is relative, foo is global in global.py and self.foo is global in Example class. If the global.py is short and clean enough (didn't have a lot of other class), they are pretty much the same. Or I missed something? One difference is that you could have many instances of Example, each with its own value of 'foo', whereas with a global 'foo' there can only be one value of 'foo' for the module. It would make sense to use the 'global' keyword if you have a module with various functions, several of which refer to 'foo', but only one of which changes the value of 'foo'. Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
I also want to know what is the difference between "using 'global variables' in a py module" and "using a variable in class". For example: In global.py: foo = 1 def bar(): global foo return foo + 1 In class.py class Example: def __init__(self): self.foo = 1 def bar() return self.foo + 1 Expect the syntax, why using class variable self.foo would be better (or more common)? I think the 'global' here is relative, foo is global in global.py and self.foo is global in Example class. If the global.py is short and clean enough (didn't have a lot of other class), they are pretty much the same. Or I missed something? Chris Angelico 于2019年8月23日周五 上午9:34写道: > On Fri, Aug 23, 2019 at 11:24 AM Windson Yang wrote: > > > > Thank you all for the great explanation, I still trying to find some good > > example to use 'global', In CPython, I found an example use 'global' in > > cpython/Lib/zipfile.py > > > > _crctable = None > > def _gen_crc(crc): > > for j in range(8): > > if crc & 1: > > crc = (crc >> 1) ^ 0xEDB88320 > > else: > > crc >>= 1 > > return crc > > > > def _ZipDecrypter(pwd): > > key0 = 305419896 > > key1 = 591751049 > > key2 = 878082192 > > > > global _crctable > > if _crctable is None: > > _crctable = list(map(_gen_crc, range(256))) > > crctable = _crctable > > > > _crctable only been used in the _ZipDecrypter function. IIUC, the code > can > > be refactored to > > > > def _gen_crc(crc): > > ...stay the same > > > > def _ZipDecrypter(pwd, _crctable=list(map(_gen_crc, range(256: > > key0 = 305419896 > > key1 = 591751049 > > key2 = 878082192 > >crctable = _crctable > > > > Which avoid using 'global' keyword. Why we are not doing this? I guess > the > > reason we use 'global' here because we don't want to create `_crctable = > > list(map(_gen_crc, range(256)))` every time when we run '_ZipDecrypter' > > function. So we kinda cache _crctable with 'global', am I right? > > It's a cache that is made ONLY when it's first needed. If you put it > in the function header, it has to be created eagerly as soon as the > module is imported. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On Fri, Aug 23, 2019 at 11:24 AM Windson Yang wrote: > > Thank you all for the great explanation, I still trying to find some good > example to use 'global', In CPython, I found an example use 'global' in > cpython/Lib/zipfile.py > > _crctable = None > def _gen_crc(crc): > for j in range(8): > if crc & 1: > crc = (crc >> 1) ^ 0xEDB88320 > else: > crc >>= 1 > return crc > > def _ZipDecrypter(pwd): > key0 = 305419896 > key1 = 591751049 > key2 = 878082192 > > global _crctable > if _crctable is None: > _crctable = list(map(_gen_crc, range(256))) > crctable = _crctable > > _crctable only been used in the _ZipDecrypter function. IIUC, the code can > be refactored to > > def _gen_crc(crc): > ...stay the same > > def _ZipDecrypter(pwd, _crctable=list(map(_gen_crc, range(256: > key0 = 305419896 > key1 = 591751049 > key2 = 878082192 >crctable = _crctable > > Which avoid using 'global' keyword. Why we are not doing this? I guess the > reason we use 'global' here because we don't want to create `_crctable = > list(map(_gen_crc, range(256)))` every time when we run '_ZipDecrypter' > function. So we kinda cache _crctable with 'global', am I right? It's a cache that is made ONLY when it's first needed. If you put it in the function header, it has to be created eagerly as soon as the module is imported. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
Thank you all for the great explanation, I still trying to find some good example to use 'global', In CPython, I found an example use 'global' in cpython/Lib/zipfile.py _crctable = None def _gen_crc(crc): for j in range(8): if crc & 1: crc = (crc >> 1) ^ 0xEDB88320 else: crc >>= 1 return crc def _ZipDecrypter(pwd): key0 = 305419896 key1 = 591751049 key2 = 878082192 global _crctable if _crctable is None: _crctable = list(map(_gen_crc, range(256))) crctable = _crctable _crctable only been used in the _ZipDecrypter function. IIUC, the code can be refactored to def _gen_crc(crc): ...stay the same def _ZipDecrypter(pwd, _crctable=list(map(_gen_crc, range(256: key0 = 305419896 key1 = 591751049 key2 = 878082192 crctable = _crctable Which avoid using 'global' keyword. Why we are not doing this? I guess the reason we use 'global' here because we don't want to create `_crctable = list(map(_gen_crc, range(256)))` every time when we run '_ZipDecrypter' function. So we kinda cache _crctable with 'global', am I right? -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 8/22/19 12:00 PM, Windson Yang wrote: > I can 'feel' that global variables are evil. I also read lots of articles > proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found > CPython Lib use quite a lot of `global` keyword. So how should we use > `global` keyword correctly? IIUC, it's fine that we use `global` keyword > inside the Lib since most of the time the user just import the lib and call > the API. In any other situation, we should avoid using it. Am I right? It should be noted that the Python 'global' is much more like the C/C++ file scoped 'static' which is less bad that fully global variables (the extern variable). -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On Fri, Aug 23, 2019 at 10:18 AM Cameron Simpson wrote: > As Michael says, "you can always read from a parent scope if the name > hasn't been used by the local scope". What this means is this: > > _MODULE_LEVEL_CACHE = {} > > def factors_of(n): > factors = _MODULE_LEVEL_CACHE.get(n) > if factors is None: > factors = factorise(n) > _MODULE_LEVEL_CACHE[n] = factors > return factors Note that this is still using a global variable, even though it isn't using the "global" keyword. The two concepts are related but distinct. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 22Aug2019 11:12, Michael Torrie wrote: On 8/22/19 10:00 AM, Windson Yang wrote: I can 'feel' that global variables are evil. I also read lots of articles proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found CPython Lib use quite a lot of `global` keyword. So how should we use `global` keyword correctly? IIUC, it's fine that we use `global` keyword inside the Lib since most of the time the user just import the lib and call the API. In any other situation, we should avoid using it. Am I right? The "global" keyword only refers to the current module as far as I know. Thus global variables are global only to the current python file, so the damage, as it were, is limited in scope. Aye. And it's only required if you plan to write to a global variable from another scope; you can always read from a parent scope if the name hasn't been used by the local scope. I'm sure there are use cases for using the global keyword. It's not evil. It's just not necessary most of the time. I don't think I've ever used the "global" keyword. If I need to share state, or simple configuration information, between modules, I place those variables in their own module file and import them where I need them. I've used it a few times. Maybe a handful of times in thousands of lines of code. As Michael says, "you can always read from a parent scope if the name hasn't been used by the local scope". What this means is this: _MODULE_LEVEL_CACHE = {} def factors_of(n): factors = _MODULE_LEVEL_CACHE.get(n) if factors is None: factors = factorise(n) _MODULE_LEVEL_CACHE[n] = factors return factors def factorise(n): ... expensive factorisation algorithm here ... Here we access _MODULE_LEVEL_CACHE directly without bothering with the global keyword. Because the function "factors_of" does not _assign_ to the name _MODULE_LEVEL_CACHE, that name is not local to the function; the outer scopes will be searched in order to find the name. Now, Python decides what variable are local to a function by staticly inspecting the code and seeing which have assignments. So: x = 9 y = 10 z = 11 function foo(x): y = 5 print(x, y, z) Within the "foo" function: - x is local (it is assigned to by the function parameter when you call it) - y is local (it is assigned to in the function body) - z is not local (it is not assigned to); the namespace searching finds it in the module scope Note that in the "factors_of" function we also do not _assign_ to _MODULE_LEVEL_CACHE. We do assign to one of its elements, but that is an access _via_ _MODULE_LEVEL_CACHE, not an assignment to the name itself. So it is nonlocal and found in the module namespace. However, where you might want the use "global" (or its modern friend "nonlocal") is to avoid accidents and to make the globalness obvious. The same example code: x = 9 y = 10 z = 11 function foo(x): y = 5 print(x, y, z) When you use a global, that is usually a very deliberate decision on your part, because using globals is _usually_ undesirable. When all variables are local, side effects are contained within the function and some surprises (== bugs) are prevented. Let's modify "foo": function foo(x): y = 5 z = y * 2 print(x, y, z) Suddenly "z" is a local variable because it is assigned to. In this function it is all very obvious because the function is very short. A longer function might not have this be so obvious. So: was "z" still intended to be global? If yes then you need the global keyword: function foo(x): global z y = 5 z = y * 2 print(x, y, z) And even if we were not assigning to "z", we might still use the "global" statement to make it obvious to the reader that "z" is a global; after all, if it not very visually distinctive - it looks a lot like "x" and "y". So my advice after all of this is: As you thought, globals are to be avoided most of the time. They invite unwanted side effects and also make it harder to write "pure functions", functions with no side effects. Pure functions (most Python functions) are much easier to reuse elsewhere. However, if you have a good case for using a global, always use the "global" statement. It has the following benefits: it makes the globalness obvious to the person reading the code and it avoids a global variable suddenly becoming local if you assign to it. (NB: the "time" of that semantic change is when you change the code, _not_ when the assignment itself happens.) Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How should we use global variables correctly?
On 8/22/19 10:00 AM, Windson Yang wrote: > I can 'feel' that global variables are evil. I also read lots of articles > proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found > CPython Lib use quite a lot of `global` keyword. So how should we use > `global` keyword correctly? IIUC, it's fine that we use `global` keyword > inside the Lib since most of the time the user just import the lib and call > the API. In any other situation, we should avoid using it. Am I right? The "global" keyword only refers to the current module as far as I know. Thus global variables are global only to the current python file, so the damage, as it were, is limited in scope. And it's only required if you plan to write to a global variable from another scope; you can always read from a parent scope if the name hasn't been used by the local scope. I'm sure there are use cases for using the global keyword. It's not evil. It's just not necessary most of the time. I don't think I've ever used the "global" keyword. If I need to share state, or simple configuration information, between modules, I place those variables in their own module file and import them where I need them. -- https://mail.python.org/mailman/listinfo/python-list
How should we use global variables correctly?
I can 'feel' that global variables are evil. I also read lots of articles proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found CPython Lib use quite a lot of `global` keyword. So how should we use `global` keyword correctly? IIUC, it's fine that we use `global` keyword inside the Lib since most of the time the user just import the lib and call the API. In any other situation, we should avoid using it. Am I right? -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Mon, Jul 15, 2019 at 5:45 AM Ian Kelly wrote: > > On Thu, Jul 11, 2019 at 11:10 PM Chris Angelico wrote: > > > > On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt > > wrote: > > > > > > Wow, I'm so sorry I answered on the question : "How do you replace a > > > variable with its value". For what i understood with the example values, > > > CrazyVideoGamez wants 3 variables named like the meal-names in > dictionary. > > > Yes, it's not secure unless you work with your own dataset (just like > > > sending your own created commands with set=True in subprocess). Yes > there > > > might be better solutions for the real problem. But maybe the user > really > > > has a purpose for it, in a secure environment with own datatset, it's a > > > valid answer for "How do you replace a variable with its value". > > > > > > > What you gave was dangerous advice, and yes, there IS a better > > solution - and an easier one. If you want to create variables > > dynamically, then just create them! > > > > for meal, parts in dinner.items(): > > globals()[meal.replace(' ','_')] = dinner[meal] > > > > Python has a rich set of metaprogramming tools. Don't just always > > reach for exec and caveat it with "it's okay if you trust everything". > > To be fair, if dinner is untrusted then this new version is still unsafe. > You've just allowed it to shadow any global or built-in it wants to. This is true, but that risk is in the original too. What I'd ACTUALLY do, in this sort of situation, would be to make some sort of namespace object, so I can write foo.Desert rather than using square bracket notation; otherwise, though, I'd just stick with the original. But if you want to unpack an object into a namespace, it's certainly better to assign directly into the namespace than to eval. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Thu, Jul 11, 2019 at 11:10 PM Chris Angelico wrote: > > On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt > wrote: > > > > Wow, I'm so sorry I answered on the question : "How do you replace a > > variable with its value". For what i understood with the example values, > > CrazyVideoGamez wants 3 variables named like the meal-names in dictionary. > > Yes, it's not secure unless you work with your own dataset (just like > > sending your own created commands with set=True in subprocess). Yes there > > might be better solutions for the real problem. But maybe the user really > > has a purpose for it, in a secure environment with own datatset, it's a > > valid answer for "How do you replace a variable with its value". > > > > What you gave was dangerous advice, and yes, there IS a better > solution - and an easier one. If you want to create variables > dynamically, then just create them! > > for meal, parts in dinner.items(): > globals()[meal.replace(' ','_')] = dinner[meal] > > Python has a rich set of metaprogramming tools. Don't just always > reach for exec and caveat it with "it's okay if you trust everything". To be fair, if dinner is untrusted then this new version is still unsafe. You've just allowed it to shadow any global or built-in it wants to. -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt wrote: > > Wow, I'm so sorry I answered on the question : "How do you replace a > variable with its value". For what i understood with the example values, > CrazyVideoGamez wants 3 variables named like the meal-names in dictionary. > Yes, it's not secure unless you work with your own dataset (just like > sending your own created commands with set=True in subprocess). Yes there > might be better solutions for the real problem. But maybe the user really > has a purpose for it, in a secure environment with own datatset, it's a > valid answer for "How do you replace a variable with its value". > What you gave was dangerous advice, and yes, there IS a better solution - and an easier one. If you want to create variables dynamically, then just create them! for meal, parts in dinner.items(): globals()[meal.replace(' ','_')] = dinner[meal] Python has a rich set of metaprogramming tools. Don't just always reach for exec and caveat it with "it's okay if you trust everything". ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
Wow, I'm so sorry I answered on the question : "How do you replace a variable with its value". For what i understood with the example values, CrazyVideoGamez wants 3 variables named like the meal-names in dictionary. Yes, it's not secure unless you work with your own dataset (just like sending your own created commands with set=True in subprocess). Yes there might be better solutions for the real problem. But maybe the user really has a purpose for it, in a secure environment with own datatset, it's a valid answer for "How do you replace a variable with its value". On Fri, Jul 12, 2019 at 12:10 PM Ben Finney wrote: > Aldwin Pollefeyt writes: > > > dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main > > Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} > > > > # Don't ask where I got the dinner from > > > > for meal in dinner.keys(): > > exec(meal.replace(' ','_') + ' = list(dinner[meal])') > > > > print(Starters) > > print(Main_Course) > > print(Desert) > > Why do you think this is needed? Why (there may be some reason! but you > have not told us what that is) can your program not just:: > > print(dinner['Starters']) > print(dinner['Main Course']) > print(dinner['Desert']) > > > OUTPUT: > > ['Fried Calamari', 'Potted crab'] > > ['Fish', 'Meat'] > > ['Cake', 'Banana Split'] > > The above code produces this output, without any need for binding new > names. So what is it you are actually trying to achieve, and why do you > think the new bindings are necessary? > > -- > \“The number of UNIX installations has grown to 10, with more | > `\ expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 | > _o__) | > Ben Finney > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
Aldwin Pollefeyt writes: > dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main > Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} > > # Don't ask where I got the dinner from > > for meal in dinner.keys(): > exec(meal.replace(' ','_') + ' = list(dinner[meal])') > > print(Starters) > print(Main_Course) > print(Desert) Why do you think this is needed? Why (there may be some reason! but you have not told us what that is) can your program not just:: print(dinner['Starters']) print(dinner['Main Course']) print(dinner['Desert']) > OUTPUT: > ['Fried Calamari', 'Potted crab'] > ['Fish', 'Meat'] > ['Cake', 'Banana Split'] The above code produces this output, without any need for binding new names. So what is it you are actually trying to achieve, and why do you think the new bindings are necessary? -- \“The number of UNIX installations has grown to 10, with more | `\ expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Fri, Jul 12, 2019 at 4:37 AM Terry Reedy wrote: > > On 7/11/2019 12:51 AM, Aldwin Pollefeyt wrote: > > dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main > > Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} > > > > # Don't ask where I got the dinner from > > > > for meal in dinner.keys(): > > exec(meal.replace(' ','_') + ' = list(dinner[meal])') > > If dinner came from an untrusted source, and the OP does not say > differently, this would be a stupid thing to do. > Yes, but if your dinner came from an untrusted source, you have bigger problems than remote code execution exploits. But exec is a bad idea even without security concerns. Python has FAR better metaprogramming features (mutating globals() comes to mind here) - but even that's probably not necessary. Needs more info from the OP. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 11/07/2019 05:51, Aldwin Pollefeyt wrote: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): exec(meal.replace(' ','_') + ' = list(dinner[meal])') print(Starters) print(Main_Course) print(Desert) OUTPUT: ['Fried Calamari', 'Potted crab'] ['Fish', 'Meat'] ['Cake', 'Banana Split'] If you think you need to do this, you are almost certainly wrong. Ew! -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 7/11/2019 12:51 AM, Aldwin Pollefeyt wrote: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): exec(meal.replace(' ','_') + ' = list(dinner[meal])') If dinner came from an untrusted source, and the OP does not say differently, this would be a stupid thing to do. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): exec(meal.replace(' ','_') + ' = list(dinner[meal])') print(Starters) print(Main_Course) print(Desert) OUTPUT: ['Fried Calamari', 'Potted crab'] ['Fish', 'Meat'] ['Cake', 'Banana Split'] On Thu, Jul 11, 2019 at 10:34 AM Ben Finney wrote: > Terry Reedy writes: > > > On 7/10/2019 6:37 PM, CrazyVideoGamez wrote: > > > > > and I'm just changing it with the code above (you can find that by > > > printing it out). How can I make separate lists called 'Starters', > > > 'Main Course', and 'Desert'? > > > > starters = dinner['Starters'] > > main_course = dinner['Main Course'] > > desert = dinner['Desert'] > > The question comes, though, why you (CrazyVideoGamez) are doing this. > > You have the lists immediately accesible as dictionary elements, by > name. > > Why do you need to also have them bound to separate names; what problem > are you trying to solve that you think this will help? > > -- > \ “If [a technology company] has confidence in their future | > `\ ability to innovate, the importance they place on protecting | > _o__) their past innovations really should decline.” —Gary Barnett | > Ben Finney > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 2019-07-11 12:43 AM, CrazyVideoGamez wrote: How do you replace a variable with its value in python 3.7.2? For example, say I have: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): meal = list(dinner[meal]) But I only get one list called "meal" and I'm just changing it with the code above (you can find that by printing it out). How can I make separate lists called 'Starters', 'Main Course', and 'Desert'? 1. Iterating over a dictionary returns each key. So instead of 'for meal in dinner.keys()' you can just say 'for meal in dinner'. 2. It is not a good idea to use the variable name 'meal' for two purposes. You use it to get each key, and then it gets over-ridden with the result of 'list(dinner[meal])'. Then on the next iteration it gets over-ridden again with the next key. 3. The result of 'dinner[meal]' is already a list, so there is no need to say 'list(dinner[meal])'. Technically there is a difference - your approach creates a new list, instead of just creating a reference to the original one, but I doubt if that was your intention. 4. There is potentially more than one list, but for each iteration you over-ride the previous one, so at the end, only the last one remains. The solution is to create a 'list of lists'. Putting all this together - courses = [] for course in dinner: courses.append(dinner[course]) This gives you a list, called 'courses', containing three lists, one for each 'course' containing the options for that course. However, in the process, you have lost the names of the courses, namely 'Starters', 'Main course', and 'Desert'. So to answer your original question "How can I make separate lists called 'Starters', 'Main Course', and 'Desert'?", the code that you started with is exactly what you asked for. I think you were asking how to create a variable called 'Starters' containing the list of starters. It can be done, using the built-in function 'setattr()', but I don't think that would be useful. If you knew in advance that one of the options was called 'Starters', you could just say Starters = ['Fried Calamari', 'Potted crab']. But if you did not know that in advance, how would you know what your variable was called? Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
Terry Reedy writes: > On 7/10/2019 6:37 PM, CrazyVideoGamez wrote: > > > and I'm just changing it with the code above (you can find that by > > printing it out). How can I make separate lists called 'Starters', > > 'Main Course', and 'Desert'? > > starters = dinner['Starters'] > main_course = dinner['Main Course'] > desert = dinner['Desert'] The question comes, though, why you (CrazyVideoGamez) are doing this. You have the lists immediately accesible as dictionary elements, by name. Why do you need to also have them bound to separate names; what problem are you trying to solve that you think this will help? -- \ “If [a technology company] has confidence in their future | `\ ability to innovate, the importance they place on protecting | _o__) their past innovations really should decline.” —Gary Barnett | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 7/10/2019 6:37 PM, CrazyVideoGamez wrote: the below twice. Please post just once and be patient for at least a couple of hours -- or days if not subscribed. How do you replace a variable with its value in python 3.7.2? Use the variable (name) in an expression. But this is not what you ask below. For example, say I have: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from I think most of us have seen examples like this in programming books. for meal in dinner.keys(): meal = list(dinner[meal] But I only get one list called "meal" Because it is just one identifier, and you rebind it to multiple values. and I'm just changing it with the code above (you can find that by printing it out). How can I make separate lists called 'Starters', 'Main Course', and 'Desert'? starters = dinner['Starters'] main_course = dinner['Main Course'] desert = dinner['Desert'] -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
How Do You Replace Variables With Their Values?
How do you replace a variable with its value in python 3.7.2? For example, say I have: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): meal = list(dinner[meal]) But I only get one list called "meal" and I'm just changing it with the code above (you can find that by printing it out). How can I make separate lists called 'Starters', 'Main Course', and 'Desert'? -- https://mail.python.org/mailman/listinfo/python-list
How Do You Replace Variables With Their Values?
How do you replace a variable with its value in python 3.7.2? For example, say I have: dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']} # Don't ask where I got the dinner from for meal in dinner.keys(): meal = list(dinner[meal] But I only get one list called "meal" and I'm just changing it with the code above (you can find that by printing it out). How can I make separate lists called 'Starters', 'Main Course', and 'Desert'? -- https://mail.python.org/mailman/listinfo/python-list
Selection based ob variables issue
Hello, iam tryting to execute mysql queries based on python variables. name = request.args.get('name') month = request.args.get('month') year = request.args.get('year') try: if '=' not in name + month + year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) ) elif '=' not in name + year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) ) elif '=' not in month + year: cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) elif '=' not in year: cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year ) else: Problem is that only the first clause works as expected and not the rest. '=' means variables contains no value. How can i make it work? -- https://mail.python.org/mailman/listinfo/python-list
Re: Deletion of Environmental Variables
On Mon, 7 Jan 2019 at 06:37, Terry Reedy wrote: > The pydev recommended way to run pip on windows is > > py -x.y pip > as this installs the package requested into the x.y site-packages > directory. py -3.7 -m pip ... Note the extra -m). Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: Deletion of Environmental Variables
On 1/6/2019 9:56 PM, Logan Vogelsong wrote: I planned on using python to simulate different cipher to challenge myself, but I kinda deleted my environmental variables to python. I run Windows 10 and wanted to get NumPy and MatPlotLib modules imported to python. Basically, I downloaded python 3.7.1 first, but when I realized it was not compatible with TensorFlow (I wanted to make a machine learning algorithm at a point in time), I uninstalled it to get python 3.6.1. Windows easily allows multiple versions to be installed. The py launcher lets you easily pick which to run (from a command console). For 3.6, use the latest release, now 3.6.8, which has hundreds of fixes since 3.6.1. If you have multiple versions, you must install 3rd party modules for each version you want to import them into. Somehow, maybe I misread a stack overflow post, but I deleted all my path files to python36 for whatever reason. I still have no idea why I did this. Then, I thought, if I could uninstall python 3.6.1 and reinstall it pip would come back and I could use python from the cmd. I uninstalled it and tried reinstalling (with chocolatey this time) it for no avail. I tried searching for all the correct environment variables to put them back in manually, but I cannot seem to find most of them since python is still “not recognized as an internal or external command, operable program or batch file.” There is, or at least used to be, an option to add the python directly to PATH. But using py instead python is needed anyway to select from multiple versions. In my cmd. I really want pip to work so I can download modules. By default, the PSF Windows installer installs pip, using the ensurepip module. If this did not happen, you can run it yourself. See https://docs.python.org/3/library/ensurepip.html The pydev recommended way to run pip on windows is > py -x.y pip as this installs the package requested into the x.y site-packages directory. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Deletion of Environmental Variables
Hello- I think I subscribed now. I planned on using python to simulate different cipher to challenge myself, but I kinda deleted my environmental variables to python. I run Windows 10 and wanted to get NumPy and MatPlotLib modules imported to python. Basically, I downloaded python 3.7.1 first, but when I realized it was not compatible with TensorFlow (I wanted to make a machine learning algorithm at a point in time), I uninstalled it to get python 3.6.1. Somehow, maybe I misread a stack overflow post, but I deleted all my path files to python36 for whatever reason. I still have no idea why I did this. Then, I thought, if I could uninstall python 3.6.1 and reinstall it pip would come back and I could use python from the cmd. I uninstalled it and tried reinstalling (with chocolatey this time) it for no avail. I tried searching for all the correct environment variables to put them back in manually, but I cannot seem to find most of them since python is still “not recognized as an internal or external command, operable program or batch file.” In my cmd. I really want pip to work so I can download modules. I may follow: https://stackoverflow.com/questions/4750806/how-do-i-install-pip-on-windows to try and help reinstall pip and I may redownload it through: https://pypi.org/project/pip/#files with help from: https://pypi.org/project/setuptools/#files As an aside, I have so far taught myself everything I know about python, and I am willing to learn even more. Thank you for any help, Logan Vogelsong -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, 23 Jul 2018 14:39:56 +0300, Marko Rauhamaa wrote: > Steven D'Aprano : > >> Lambda calculus has the concept of a binding operator, which is >> effectively an assignment operator: it takes a variable and a value and >> binds the value to the variable, changing a free variable to a bound >> variable. In other words, it assigns the value to the variable, just >> like assignment does. > > In traditional Lambda Calculus semantics, there are no values at all. It is more common to say "pure lambda calculus" rather than "traditional", and it is not correct to say there are no values at all. Rather, all values are functions (and all functions are values). http://scienceblogs.com/goodmath/2006/08/29/a-lambda-calculus-rerun-1/ and: "As this suggests, functions are just ordinary values, and can be the results of functions or passed as arguments to functions (even to themselves!). Thus, in the lambda calculus, functions are first-class values. Lambda terms serve both as functions and data." http://www.cs.cornell.edu/courses/cs6110/2013sp/lectures/lec02-sp13.pdf And from the same notes: "So, what is a value? In the pure lambda calculus, any abstraction is a value. Remember, an abstraction λx:e is a function; in the pure lambda calculus, the only values are functions. In an applied lambda calculus with integers and arithmetic operations, values also include integers. Intuitively, a value is an expression that can not be reduced/executed/simplified any further." [...] > The lambda calculus comment is just an aside. The main point is that you > shouldn't lead people to believe that Python has variables that are any > different than, say, Pascal's variables (even if you, for whatever > reason, want to call them "names"). They are memory slots that hold > values until you assign new values to them. Nevertheless, they are still different. My computer has an ethernet slot and a USB slot, and while they are both slots that hold a cable and transmit information in and out of the computer, they are nevertheless different. The differences are just as important as the similarities. > It *is* true that Python has a more limited data model than Pascal (all > of Python's values are objects in the heap and only accessible through > pointers). Calling it "more limited" is an inaccurate and pejorative way of putting it. Rather, I would say it is a more minimalist, *elegant* data model: * a single kind of variable (objects in the heap where the interpreter manages the lifetime of objects for you) as opposed to Pascal's more complex and more difficult model: * two kinds of variables: - first-class variables that the compiler manages for you (allocating and deallocating them on the stack) - second-class variables that the programmer has to manage manually (declaring pointers, allocating memory by hand, tracking the lifetime of the memory block yourself, deallocating it when you are done, and carefully avoiding accessing the pointed-to memory block after deallocation). At least more modern languages with both value-types and reference-types (such as Java, C#, Objective C, Swift) manage to elevate their reference- type variables to first-class citizenship. > Also, unlike Pascal, variables can hold (pointers to) values > of any type. IOW, Python has the data model of Lisp. > > Lisp talks about binding and rebinding variables as well: > >https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node79.html> > > which might be Lambda Calculus legacy, but at least they are not shy to > talk about variables and assignment. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, Jul 23, 2018 at 9:39 PM, Steven D'Aprano wrote: > [1] The CPython interpreter uses pointers; the Jython interpreter uses > whatever kind of memory indirection the JVM provides; when I emulate a > Python interpreter using pencil and paper, there's not a pointer in sight > but a lot of copying of values and crossing them out. ("Copy on access" > perhaps?) A Python interpreter emulated by a Turing machine would use > dots on a long paper tape, and an analog computer emulating Python would > use I-have-no-idea. Clockwork? Hydraulics? I've been known to implement a linked list using a deck of cards, with tetrapod "one teaspoon of sugar" packets for the Next markers, and pens for externally-accessible references (the head of the list, the current node, etc). This therefore proves that a pointer IS a teaspoon of sugar, and vice versa. Would you like your tea with one pointer or two? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, 23 Jul 2018 09:22:55 +0300, Marko Rauhamaa wrote: > Dennis Lee Bieber : [...] >> In my world, Java and Python are the ones that are not "common". > > Yes, "boxed" is a Java term. However, the programming pattern of using > dynamic memory and pointers is ubiquitous and ancient: Not that ancient -- the first version(s) of Fortran didn't have dynamic memory allocation or pointers. (Admittedly, Lisp did follow not long afterwards.) But it is certainly not ubiquitous: many languages don't have pointers at all. > FILE *f = fopen("xyz", "r"); > > where f holds a pointer, fopen() returns a pointer, and "xyz" and "r" > evaluate to pointer values. > > In Python, every expression evaluates to a pointer and every variable > holds a pointer. Within the semantics of the Python language, there are no pointer values, no way to get a pointer to a memory location or a pointer to an object. No expression in Python evaluates to a pointer, no variables hold pointers in Python. The Python language is defined in terms of objects: expressions evaluate to objects, and variables are names bound to objects. If you don't believe me, believe the interpreter: # Marko expects a pointer, but unfortunately he gets an int py> type(1 + 2) Marko is making a similar category error as those who insist that Python uses "call by reference" or "call by value" for parameter passing. He mistakes an irrelevant implementation detail used by *some* but not all Python interpreters[1] for entities which exist in the Python computation model. As Fredrick puts it: "Joe, I think our son might be lost in the woods" "Don't worry, I have his social security number" http://effbot.org/zone/call-by-object.htm (The *pointer to an object* used in the implementation is not the same as the object itself.) Evaluating 1 + 2 gives the value (an object) 3, not a pointer to the value 3. Pointers are not merely "not first-class citizens" of Python, they aren't citizens at all: there is nothing we can do in pure Python to get hold of pointers, manipulate pointers, or dereference pointers. https://en.wikipedia.org/wiki/First-class_citizen Pointers are merely one convenient, useful mechanism to implement Python's model of computation in an efficient manner on a digital computer. They are not part of the computation model, and pointers are not values available to the Python programmer[2]. [1] The CPython interpreter uses pointers; the Jython interpreter uses whatever kind of memory indirection the JVM provides; when I emulate a Python interpreter using pencil and paper, there's not a pointer in sight but a lot of copying of values and crossing them out. ("Copy on access" perhaps?) A Python interpreter emulated by a Turing machine would use dots on a long paper tape, and an analog computer emulating Python would use I-have-no-idea. Clockwork? Hydraulics? https://en.wikipedia.org/wiki/MONIAC https://makezine.com/2012/01/24/early-russian-hydraulic-computer/ [2] Except by dropping into ctypes or some other interface to the implementation, and even then the pointers have to be converted to and from int objects as they cross the boundary between the Python realm and the implementation realm. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Steven D'Aprano : > Lambda calculus has the concept of a binding operator, which is > effectively an assignment operator: it takes a variable and a value > and binds the value to the variable, changing a free variable to a > bound variable. In other words, it assigns the value to the variable, > just like assignment does. In traditional Lambda Calculus semantics, there are no values at all. There are only well-formatted formulas and syntactic transformation rules. You could view it as a macro preprocessing system where you keep transforming the formula until no transformation rule applies. Yes, λ can be viewed as a binding operator although classically, it is simply a dead symbol just like '(', '.' and 'z'. > Especially in this case. Anyone who understands lambda calculus is > unlikely to be confused by Python using the same terms to mean > something *almost identical* to what they mean in lambda calculus. > (The only difference I can see is that lambda calculus treats > variables as abstract mathematical entities, while Python and other > programming languages vivify them and give them a concrete > implementation.) > > If one in ten thousand programmers are even aware of the existence of > lambda calculus, I would be surprised. To give up using perfectly > good, accurate terminology in favour of worse, less accurate > terminology in order to avoid unlikely and transient confusion among a > minuscule subset of programmers seems a poor tradeoff to me. The lambda calculus comment is just an aside. The main point is that you shouldn't lead people to believe that Python has variables that are any different than, say, Pascal's variables (even if you, for whatever reason, want to call them "names"). They are memory slots that hold values until you assign new values to them. It *is* true that Python has a more limited data model than Pascal (all of Python's values are objects in the heap and only accessible through pointers). Also, unlike Pascal, variables can hold (pointers to) values of any type. IOW, Python has the data model of Lisp. Lisp talks about binding and rebinding variables as well: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node79.html> which might be Lambda Calculus legacy, but at least they are not shy to talk about variables and assignment. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, 23 Jul 2018 11:49:37 +0300, Marko Rauhamaa wrote: > People new to Python are unnecessarily confused by talking about names > and binding when it's really just ordinary variables and assignment. It really isn't, not to those people who expect ordinary variables and assignment to be the same as that of C, C++, C#, Objective C, Swift, Pascal, Java, Go etc. There are at least two common models for the association between symbolic names and values in programming: 1. variables are named boxes at a statically-allocated, fixed location in memory, usually on the stack ("value types"); 2. variables are names that refer to dynamically-allocated objects in the heap, often movable ("reference types"). It is absolutely true that both are "variables" of a kind, and that "name binding" is abstract enough to refer to both models. But in *practice*, the influence of Algol, C and BASIC especially is so great that many people think of variables and assignment exclusively in the first sense. Since Python uses the second sense, having a distinct name to contrast the two is desirable, and "name binding" seems to fit that need. I no longer believe that we should actively avoid the word "variable" when referring to Python. I think that's an extreme position which isn't justified. But "name binding" is an accurate technical term and not that hard to understand (on a scale of 0 to "monad", it's about 1) and I think it is elitist to claim that "people new to Python"[1] will necessarily be confused and we therefore ought to avoid the term. There are lots of confusing terms and concepts in Python. People learn them. Name binding is no different. [1] What, all of them? Even those with a comp sci PhD and 40 years programming experience in two dozen different languages? -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Mark Lawrence writes: > On 22/07/18 14:51, Abdur-Rahmaan Janhangeer wrote: >> except local vars >> >> Abdur-Rahmaan Janhangeer >> https://github.com/Abdur-rahmaanJ >> Mauritius >> > > Python doesn't have variables, it has names. I think we should cut beginners (and probably everyone) some slack about this. I don't know if work is underway to purge the term from the Python documentation, but until that is done people can be forgiven for thinking that the term is acceptable. For example, https://docs.python.org/3/tutorial/classes.html says "The global statement can be used to indicate that particular variables live in the global scope and should be rebound there; the nonlocal statement indicates that particular variables live in an enclosing scope and should be rebound there." and https://www.python.org/dev/peps/pep-0526/ is titled "Syntax for Variable Annotations". It describes: "This PEP aims at adding syntax to Python for annotating the types of variables (including class variables and instance variables), instead of expressing them through comments" -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, 23 Jul 2018 20:24:30 +1200, Gregory Ewing wrote: > Steven D'Aprano wrote: >> So let me see if I understand your argument... >> >> - we should stop using the term "binding", because it means >> nothing different from assignment; >> - binding (a.k.a. "assignment") comes from lambda calculus; >> - which has no assignment (a.k.a. "binding"). > > No, that's not what Marko is saying at all. He's pointing out that the > term "binding" means something completely different in lambda calculus. Well done in reading Marko's intent. Unfortunately, I'm not as good as inferring meaning as you seem to be, consequently I had to judge by what he wrote, not what he meant. When a writer fails to communicate their intent, that's usually the failure of the writer, not the reader. We aren't mind-readers and writers should not blame the reader when they fail to communicate their intended meaning. > The terms "bound variable" and "free variable" in lambda calculus mean > what in Python we would call a "local variable" vs. a "non-local > variable". Actually, no, they are called "bound variable" and "free variable" in Python too. https://docs.python.org/3/reference/executionmodel.html See also: http://effbot.org/zone/closure.htm Alas, I don't think Fredrik Lundh got it *quite* right. I think that globals (and builtins) in Python are "open free variables", as opposed to nonlocals which are closed. And sadly, the Python glossary currently doesn't define free variables nor bound variables, or even name binding. > They have nothing to do with assignment at all. That's not quite correct either. Lambda calculus has the concept of a binding operator, which is effectively an assignment operator: it takes a variable and a value and binds the value to the variable, changing a free variable to a bound variable. In other words, it assigns the value to the variable, just like assignment does. In Python terms, = is a binary binding operator: it takes a left hand operand, the variable (a name, for the sake of simplicity) and a right hand operand (a value) and binds the value to the name. > Marko is asking us to stop using the word "binding" to refer to > assignment because of the potential confusion with this other meaning. Marko has some idiosyncratic beliefs about Python (and apparently other languages as well) that are difficult to justify. Especially in this case. Anyone who understands lambda calculus is unlikely to be confused by Python using the same terms to mean something *almost identical* to what they mean in lambda calculus. (The only difference I can see is that lambda calculus treats variables as abstract mathematical entities, while Python and other programming languages vivify them and give them a concrete implementation.) If one in ten thousand programmers are even aware of the existence of lambda calculus, I would be surprised. To give up using perfectly good, accurate terminology in favour of worse, less accurate terminology in order to avoid unlikely and transient confusion among a minuscule subset of programmers seems a poor tradeoff to me. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
"Marko Rauhamaa" wrote in message news:87zhyitjz2@elektro.pacujo.net... People new to Python are unnecessarily confused by talking about names and binding when it's really just ordinary variables and assignment. It seems to be mostly some sort of marketing lingo that seeks to create an air of mystique around Python. I don't think that is a fair comment. I am not qualified to enter the debate itself, but as an 'outsider' I can offer two thoughts - 1. It seems that, whatever terminology we come up with, some other language will use similar terminology, but with a subtly different meaning. Newcomers to Python coming from that other language often get confused because they make certain assumptions, based on their other experience, which turn out to be unfounded in Python. 2. My 'aha' moment came early on when I read somewhere that Python objects have 3 attributes - a type, a value, and a unique id. One thing that they do *not* have is a name. Once I understood that, a lot of things became clearer. Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Ben Finney : > Gregory Ewing writes: > >> Marko is asking us to stop using the word "binding" to refer to >> assignment because of the potential confusion with this other meaning. > > That's about as reasonable as my request that we stop using the term > “variable” for what is, in Python, an un-typed reference to an object. > > I expect both of these requests to meet with little satisfaction. I'm actually not asking, only wishing. People new to Python are unnecessarily confused by talking about names and binding when it's really just ordinary variables and assignment. It seems to be mostly some sort of marketing lingo that seeks to create an air of mystique around Python. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Gregory Ewing writes: > Marko is asking us to stop using the word "binding" to refer to > assignment because of the potential confusion with this other meaning. That's about as reasonable as my request that we stop using the term “variable” for what is, in Python, an un-typed reference to an object. I expect both of these requests to meet with little satisfaction. -- \ “We are all agreed that your theory is crazy. The question that | `\ divides us is whether it is crazy enough to have a chance of | _o__)being correct.” —Niels Bohr (to Wolfgang Pauli), 1958 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Steven D'Aprano wrote: So let me see if I understand your argument... - we should stop using the term "binding", because it means nothing different from assignment; - binding (a.k.a. "assignment") comes from lambda calculus; - which has no assignment (a.k.a. "binding"). No, that's not what Marko is saying at all. He's pointing out that the term "binding" means something completely different in lambda calculus. The terms "bound variable" and "free variable" in lambda calculus mean what in Python we would call a "local variable" vs. a "non-local variable". They have nothing to do with assignment at all. Marko is asking us to stop using the word "binding" to refer to assignment because of the potential confusion with this other meaning. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Dennis Lee Bieber : > On Mon, 23 Jul 2018 00:08:00 +0300, Marko Rauhamaa > declaimed the following: > >>I Java terms, all Python values are boxed. That's a very usual pattern >>in virtually all programming languages (apart from FORTRAN). > > FORTRAN, C, COBOL, BASIC, Pascal, ALGOL, BCPL, REXX, VMS DCL, probably > R, Matlab, APL. > > I never encountered the term "boxed" until trying to read some of the > O'Reilly books on Java. > > In my world, Java and Python are the ones that are not "common". Yes, "boxed" is a Java term. However, the programming pattern of using dynamic memory and pointers is ubiquitous and ancient: FILE *f = fopen("xyz", "r"); where f holds a pointer, fopen() returns a pointer, and "xyz" and "r" evaluate to pointer values. In Python, every expression evaluates to a pointer and every variable holds a pointer. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
variables here refers to the user experience of a var np, just a link to the thread/s would mark the end of it Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Mauritius -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On 22/07/18 14:51, Abdur-Rahmaan Janhangeer wrote: except local vars Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Mauritius Python doesn't have variables, it has names. Please can we avoid a long meaningless thread on this subject as it's been discussed a trillion times before. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Sun, 22 Jul 2018 17:50:06 -0400, Dennis Lee Bieber wrote: > On Mon, 23 Jul 2018 00:08:00 +0300, Marko Rauhamaa > declaimed the following: > >>I Java terms, all Python values are boxed. That's a very usual pattern >>in virtually all programming languages (apart from FORTRAN). >> >> > FORTRAN, C, COBOL, BASIC, Pascal, ALGOL, BCPL, REXX, VMS DCL, > probably R, Matlab, APL. > > I never encountered the term "boxed" until trying to read some of > the O'Reilly books on Java. > > In my world, Java and Python are the ones that are not "common". Indeed. Its not just older languages from the 60s and 70s with value-type variables. Newer languages intended as systems languages, like Rust and Go, do the same. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, 23 Jul 2018 00:08:00 +0300, Marko Rauhamaa wrote: > Would you call it binding in this case: > >X[0]["z"] = getit() >X[3]["q"] = X[0]["z"] >X[0]["z"].changeit() It is a binding, but it is not a *name* binding. Since we are talking about name bindings, and comparing/contrasting them to variable assignment in classical languages, I don't think that binding to slots in hash tables or arrays is relevant except to muddy the waters and make things more complicated than they need be. > I think what you are talking about is more usually called "referencing." I don't think so. Its certainly not a term I've ever heard in this context before. >> With a language with more ‘classical’ variable, the assignment of Y = X >> would normal make a copy of that object, so the value Y does not get >> changed by X.changeit(). > > I Java terms, all Python values are boxed. Correct. Java mixes two different models of variable assignment: it uses classical C- and Pascal-like variable assignment for primitive values, and Lisp- and Smalltalk-like name binding for boxed values (objects), leading to two distinct sets of behaviour. That makes Java a good lesson in why it is useful to distinguish between two models of name binding. Java is not the only language with the distinction between "value types" (primitive values usually stored on the stack) and "reference types" (usually objects stored in the heap). C# and other .Net languages often make that distinction: http://net-informations.com/faq/general/valuetype-referencetype.htm Swift is another such language. Other languages which use primarily or exclusively value-types (i.e. the "variables are a named box at a fixed memory location" model) include Algol, Pascal, Modula-3, C, C++, C#, Objective C, D, Swift, COBOL, Forth, Ada, PL/I, Rust and many others. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Sun, 22 Jul 2018 22:50:52 +0300, Marko Rauhamaa wrote: > I wish people stopped talking about "name binding" and "rebinding," > which are simply posh synonyms for variable assignment. Properly, the > term "binding" comes from lambda calculus, whose semantics is defined > using "bound" and "free" variables. Lambda calculus doesn't have > assignment. So let me see if I understand your argument... - we should stop using the term "binding", because it means nothing different from assignment; - binding (a.k.a. "assignment") comes from lambda calculus; - which has no assignment (a.k.a. "binding"). Which leads us to the conclusion that lambda calculus both has and doesn't have binding a.k.a. assignment at the same time. Perhaps it is a quantum phenomenon. Are you happy with the contradiction inherent in your statements, or would you prefer to reword your argument? -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Richard Damon : >> On Jul 22, 2018, at 3:50 PM, Marko Rauhamaa wrote: >> I wish people stopped talking about "name binding" and "rebinding," >> which are simply posh synonyms for variable assignment. Properly, the >> term "binding" comes from lambda calculus, whose semantics is defined >> using "bound" and "free" variables. Lambda calculus doesn't have >> assignment. > > Marko, I think the term binding makes sense in python due to how names > work. In python and the following code: > > X = getit() > Y = X > X.changeit() > > In python, presuming getit() returns some form of object (so it has a > changeit() member) then X and Y are bound to the same object, and > changeit() will thus also affect the object that we see at Y. Would you call it binding in this case: X[0]["z"] = getit() X[3]["q"] = X[0]["z"] X[0]["z"].changeit() I think what you are talking about is more usually called "referencing." > With a language with more ‘classical’ variable, the assignment of Y = > X would normal make a copy of that object, so the value Y does not get > changed by X.changeit(). I Java terms, all Python values are boxed. That's a very usual pattern in virtually all programming languages (apart from FORTRAN). Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Mon, Jul 23, 2018 at 6:14 AM, Marko Rauhamaa wrote: > While FORTRAN or C couldn't operate on functions like this, an assembly > language program could easily. Simply compose a CPU instruction sequence > on the fly, mark it executable and use the "CALL" opcode to transfer > control to your constructed function. ... right up until the point where you realize that this is a massive security vulnerability, so you get a segmentation fault (or "protection fault" under Windows) for trying to execute a non-executable segment. > In the same vein, you could understand the "def" statement as a runtime > compiler that takes the function body, compiles it into machine language > and assigns the start address to the given variable. In fact, that would > be a perfectly working way to implement "def." Whether it would be a > smart thing to do is a different question. Key is, though, that "def" > always creates a new *data* object that can be called as *executable > code*. Here's a simpler way to describe it: The 'def' statement creates a function and assigns it to the name given. Tada! No need to talk about compilers and addresses and stuff, which are utterly irrelevant in Python. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
Bart : > If you did need one of those others to be variable, then you just assign > it to a variable the rare times you need to do that. For example: > > def fn1(): pass > def fn2(): pass > > fn = fn1 if cond else fn2 > > fn1, fn2 will always be functions. fn will always be a variable, but one > that can change between referring to fn1, fn2 or anything else. In high-level programming languages, functions are ordinary values. You can perform similar operations on functions as on integers or strings. You can give me two functions, and I can use those two to create a third function: def compose(f1, f2): def composition(x): return f1(f2(x)) return composition Here "compose", "composition", "f1" and "f2" are variables: * "compose" gets assigned when the first, outer "def" statement is executed, * "f1" and "f2" get assigned when the function held by "compose" is called, * "composition" gets assigned when the inner "def" statement is executed. While FORTRAN or C couldn't operate on functions like this, an assembly language program could easily. Simply compose a CPU instruction sequence on the fly, mark it executable and use the "CALL" opcode to transfer control to your constructed function. In the same vein, you could understand the "def" statement as a runtime compiler that takes the function body, compiles it into machine language and assigns the start address to the given variable. In fact, that would be a perfectly working way to implement "def." Whether it would be a smart thing to do is a different question. Key is, though, that "def" always creates a new *data* object that can be called as *executable code*. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
> On Jul 22, 2018, at 3:50 PM, Marko Rauhamaa wrote: > I wish people stopped talking about "name binding" and "rebinding," > which are simply posh synonyms for variable assignment. Properly, the > term "binding" comes from lambda calculus, whose semantics is defined > using "bound" and "free" variables. Lambda calculus doesn't have > assignment. > > More about variable binding here: https://en.wikipedia.org/wiki/Free_variables_and_bound_variables> > > > Marko Marko, I think the term binding makes sense in python due to how names work. In python and the following code: X = getit() Y = X X.changeit() In python, presuming getit() returns some form of object (so it has a changeit() member) then X and Y are bound to the same object, and changeit() will thus also affect the object that we see at Y. With a language with more ‘classical’ variable, the assignment of Y = X would normal make a copy of that object, so the value Y does not get changed by X.changeit(). -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
r...@zedat.fu-berlin.de (Stefan Ram): >>Rebinding names is near-universal in programming, but usually names >>that are intended to be rebound, such as variables. > > To someone like me who has grown up with a LISP 1 > this is completely natural. > > |>( SETQ A ( LAMBDA () 'ALPHA )) > |(LAMBDA () 'ALPHA) "Setq" is the dirty little secret of LISP. Scheme marks its shameful primitives with an exclamation mark. Thus, its assignment primitive is "set!". I wish people stopped talking about "name binding" and "rebinding," which are simply posh synonyms for variable assignment. Properly, the term "binding" comes from lambda calculus, whose semantics is defined using "bound" and "free" variables. Lambda calculus doesn't have assignment. More about variable binding here: https://en.wikipedia.org/wiki/Free_variables_and_bound_variables> Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On 2018-07-22 09:36:13 -0400, Richard Damon wrote: > > On Jul 22, 2018, at 8:48 AM, Sharan Basappa > > wrote: > > > > In other programming languages (e.g. C, C++), as a good practice, > > variables are declared right at the start of the program, There is no "start of the program" in C or C++. I assume that "start of the function" was meant since "start of the compilation unit" would mean that all variables are global (at least within the compilation unit) which hopefully nobody considers a good practice. > > irrespective of where it is normally used. What is the practice in > > Python? > > > > I see that, most of the code, declare variables where it is used and > > not at the start of the program. > > I would disagree that it is universally considered good practice to > declare everything at the front. (There is much disagreement on this, > but most consider declare at first use to be preferred, where > possible). > Last century, C code required this to make things easier on the > compiler, Nope. The language description in the German translation of K&R I (1984) already shows declarations at the beginning of each block, not just each function. The main text doesn't seem to mention this and all examples declare variables at the start of each function, so it is possible that this feature was added between the release of the original (1978) and the German translation. But in any case block scope existed by 1984, well before the turn of the century. So the authors of C considered block scoped variables desirable from the beginning or at least added them quite early. I would therefore assume that they considered declaring variables in a block as good practice. C++ introduced the possibility to declare variables at any point in a block, not just the beginning. C copied this in C99. Again I would argue that Stroustrup introduced the feature because he considered declaring variables for the smallest possible scope as good practice, and that the C committee copied it because they agreed. hp -- _ | Peter J. Holzer| we build much bigger, better disasters now |_|_) || because we have much more sophisticated | | | h...@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/> signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
except local vars Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Mauritius -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
On Sun, Jul 22, 2018 at 10:48 PM, Sharan Basappa wrote: > In other programming languages (e.g. C, C++), as a good practice, variables > are declared right at the start of the program, irrespective of where it is > normally used. What is the practice in Python? > > I see that, most of the code, declare variables where it is used and not at > the start of the program. Common practice in Python is to never declare your variables, since Python doesn't have variable declarations. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: coding style - where to declare variables
> On Jul 22, 2018, at 8:48 AM, Sharan Basappa wrote: > > In other programming languages (e.g. C, C++), as a good practice, variables > are declared right at the start of the program, irrespective of where it is > normally used. What is the practice in Python? > > I see that, most of the code, declare variables where it is used and not at > the start of the program. I would disagree that it is universally considered good practice to declare everything at the front. (There is much disagreement on this, but most consider declare at first use to be preferred, where possible). Last century, C code required this to make things easier on the compiler, and some code bases and examples go back that far, so they use that method out of inertia. -- https://mail.python.org/mailman/listinfo/python-list
coding style - where to declare variables
In other programming languages (e.g. C, C++), as a good practice, variables are declared right at the start of the program, irrespective of where it is normally used. What is the practice in Python? I see that, most of the code, declare variables where it is used and not at the start of the program. -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/28/18 16:44, Steven D'Aprano wrote: I agree with you that it's a bad idea. Aside from the little fact that you described concerns about using Python code for settings as "silly". Umm, no. I said that worrying about arbitrary code execution in an interpreted language seemed silly. Please be more accurate in your paraphrases. Data validation is a red herring: it is no more or less necessary to validate user settings regardless of their source. Whether they come from reading an INI file or from importing a Python file, you still need to check that they have valid values. You are making a strawman argument, since you are (again) misrepresenting what I said. Therefore, I will give you no more opportunities. -Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On Thu, 28 Jun 2018 10:58:36 -0700, Jim Lee wrote: > On 06/28/18 07:30, Grant Edwards wrote: >> I still maintain it's a bad idea to run arbitrary code found in >> user-edited config files. >> >> There may be cases where somebody has figured out how to muck with a >> config file that's shared among multiple users, or has tricked somebody >> into including something from an untrusted source in an include file. >> >> Or there could be users who don't know what they're doing and >> unwittingly type something harmful into a config file: >> >>bad_command = os.system("rm -rf ~/*") >> >> Yes, I know, users would never be that dumb... >> > I agree with you that it's a bad idea. Aside from the little fact that you described concerns about using Python code for settings as "silly". > I was pointing out that I look > at it from an input validation viewpoint rather than a security > viewpoint - that's all. You have made it abundantly clear that you aren't thinking about security. > Absolute security isn't a solvable problem. It isn't even a technical > problem. But that's a discussion for another time... Nobody is talking about "absolute security". We're talking about *one* aspect of security: given the need to collect user-supplied settings, is it acceptable to get the settings from executable Python code? Data validation is a red herring: it is no more or less necessary to validate user settings regardless of their source. Whether they come from reading an INI file or from importing a Python file, you still need to check that they have valid values. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/28/18 07:30, Grant Edwards wrote: I still maintain it's a bad idea to run arbitrary code found in user-edited config files. There may be cases where somebody has figured out how to muck with a config file that's shared among multiple users, or has tricked somebody into including something from an untrusted source in an include file. Or there could be users who don't know what they're doing and unwittingly type something harmful into a config file: bad_command = os.system("rm -rf ~/*") Yes, I know, users would never be that dumb... I agree with you that it's a bad idea. I was pointing out that I look at it from an input validation viewpoint rather than a security viewpoint - that's all. Absolute security isn't a solvable problem. It isn't even a technical problem. But that's a discussion for another time... -Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/28/18 00:46, Steven D'Aprano wrote: Yes, attacks by trusted insiders are the hardest to defend against. Betrayal of trust sucks. Trusted users with sufficient privileges could just modify the source code of your application or of Python itself. They could also attack your system in a thousand different ways. But what about untrusted users with fewer privileges? They *can't* modify the source code of your application, or change the password on other accounts, or read arbitrary files, or masquerade as other users. Because they have unprivileged accounts. So why give them the ability to escalate their privilege to that of your application (which probably can do lots of things they can't do) by directly executing Python code they supply? ??? I don't follow. I never suggested allowing someone the ability to directly execute user-supplied Python code. However, if they have the privileges necessary to run the application, I don't see the security risk. Many applications have embedded scripting engines that do just that. Your argument is akin to: "I gave my partner a key to my house, and they could rob me blind if they want. Since I trust them not to, there's no point in locking the door to the house when I go out, since they have a key." Not exactly. The original question was about reading config variables from a file in Python. That sort of thing didn't suggest (to me) a world-facing web app or other security-conscious situation. It's more like leaving the door unlocked while I'm home... -Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 2018-06-28, Steven D'Aprano wrote: > So why give them the ability to escalate their privilege to that of > your application (which probably can do lots of things they can't > do) by directly executing Python code they supply? To be fair, that situation isn't common. The vast majority of applications run with the exact same set of privledges as the user who invoked them. At least that's the case on Linux/Unix. Perhaps Windows apps are different and the usual case is for many applications to have dangerous capabilities that an average user who's invoking them shouldn't have. That sounds stupid enough to be something that would be normal for Windows. I still maintain it's a bad idea to run arbitrary code found in user-edited config files. There may be cases where somebody has figured out how to muck with a config file that's shared among multiple users, or has tricked somebody into including something from an untrusted source in an include file. Or there could be users who don't know what they're doing and unwittingly type something harmful into a config file: bad_command = os.system("rm -rf ~/*") Yes, I know, users would never be that dumb... -- Grant Edwards grant.b.edwardsYow! Everybody gets free at BORSCHT! gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On Wed, 27 Jun 2018 16:09:09 -0700, Jim Lee wrote: > On 06/27/18 15:19, Steven D'Aprano wrote: >> On Wed, 27 Jun 2018 12:15:23 -0700, Jim Lee wrote: >> >>> It seems a bit silly to me to worry about arbitrary code >>> execution in >>> an interpreted language like Python whose default runtime execution >>> method is to parse the source code directly. An attacker would be far >>> more likely to simply modify the source to achieve his ends rather >>> than try to inject a payload externally. >> Spoken like a single user on a single-user machine who has >> administrator privileges and can write to anything anywhere. >> >> >> > ...which is exactly the case I was trying to illustrate. Another is the > elevation of privileges (in a multi-user environment) due to any of a > number of methods. The point is that the source code exists in the > execution environment, and once one gains access to that code, one > doesn't *need* anything else. o_O Yes, attacks by trusted insiders are the hardest to defend against. Betrayal of trust sucks. Trusted users with sufficient privileges could just modify the source code of your application or of Python itself. They could also attack your system in a thousand different ways. But what about untrusted users with fewer privileges? They *can't* modify the source code of your application, or change the password on other accounts, or read arbitrary files, or masquerade as other users. Because they have unprivileged accounts. So why give them the ability to escalate their privilege to that of your application (which probably can do lots of things they can't do) by directly executing Python code they supply? Your argument is akin to: "I gave my partner a key to my house, and they could rob me blind if they want. Since I trust them not to, there's no point in locking the door to the house when I go out, since they have a key." -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/27/18 15:19, Steven D'Aprano wrote: On Wed, 27 Jun 2018 12:15:23 -0700, Jim Lee wrote: It seems a bit silly to me to worry about arbitrary code execution in an interpreted language like Python whose default runtime execution method is to parse the source code directly. An attacker would be far more likely to simply modify the source to achieve his ends rather than try to inject a payload externally. Spoken like a single user on a single-user machine who has administrator privileges and can write to anything anywhere. ...which is exactly the case I was trying to illustrate. Another is the elevation of privileges (in a multi-user environment) due to any of a number of methods. The point is that the source code exists in the execution environment, and once one gains access to that code, one doesn't *need* anything else. -Jim -Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On Wed, 27 Jun 2018 12:15:23 -0700, Jim Lee wrote: > It seems a bit silly to me to worry about arbitrary code execution > in > an interpreted language like Python whose default runtime execution > method is to parse the source code directly. An attacker would be far > more likely to simply modify the source to achieve his ends rather than > try to inject a payload externally. Spoken like a single user on a single-user machine who has administrator privileges and can write to anything anywhere. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
i think variables also in the case of PORT = 12345 Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ And it doesn't require that the end user have any knowlege of Python > syntax or sematics. > -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 2018-06-27, Jim Lee wrote: > It seems a bit silly to me to worry about arbitrary code > execution in an interpreted language like Python whose default > runtime execution method is to parse the source code directly. Maybe it's not a deliberate attack. Good application design is also about preventing accidents. > An attacker would be far more likely to simply modify the source to > achieve his ends rather than try to inject a payload externally. That's true if the user has write permission for the program itself. That's not how applications are usually installed (at least not on the OSes I use). > These days, "execute arbitrary code" implies a deliberate attack. Perhaps I should have phrased it differently: I didn't mean to restrict my comments to a deliberate attack. > Now, if you used input validation as an argument, I would agree that > configparser is, if not safer, easier. And it doesn't require that the end user have any knowlege of Python syntax or sematics. -- Grant Edwards grant.b.edwardsYow! ... I want FORTY-TWO at TRYNEL FLOATATION SYSTEMS gmail.cominstalled within SIX AND A HALF HOURS!!! -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/27/2018 12:15 PM, Jim Lee wrote: On 06/27/18 11:45, Abdur-Rahmaan Janhangeer wrote: and that closes it, thanks !!! Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Importing variables from a file is dangerous because it can execute arbitrary code. It should never be done with files provided by the user. Using configparser is far, far safer. It seems a bit silly to me to worry about arbitrary code execution in an interpreted language like Python whose default runtime execution method is to parse the source code directly. An attacker would be far more likely to simply modify the source to achieve his ends rather than try to inject a payload externally. These days, "execute arbitrary code" implies a deliberate attack. Now, if you used input validation as an argument, I would agree that configparser is, if not safer, easier. -Jim Not at all. Because if you're assuming a malicious user (who wasn't the one to install it), then you're assuming a multi-user environment. In which case the malicious user wouldn't have modify access to the code, unless your program says "Hey, Mal E. Factor, why don't you run your arbitrary code in my environment?" -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 06/27/18 11:45, Abdur-Rahmaan Janhangeer wrote: and that closes it, thanks !!! Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Importing variables from a file is dangerous because it can execute arbitrary code. It should never be done with files provided by the user. Using configparser is far, far safer. It seems a bit silly to me to worry about arbitrary code execution in an interpreted language like Python whose default runtime execution method is to parse the source code directly. An attacker would be far more likely to simply modify the source to achieve his ends rather than try to inject a payload externally. These days, "execute arbitrary code" implies a deliberate attack. Now, if you used input validation as an argument, I would agree that configparser is, if not safer, easier. -Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
and that closes it, thanks !!! Abdur-Rahmaan Janhangeer https://github.com/Abdur-rahmaanJ Importing variables from a file is dangerous because it can execute > arbitrary code. It should never be done with files provided by the > user. > > Using configparser is far, far safer. > -- https://mail.python.org/mailman/listinfo/python-list
Re: configparser v/s file variables
On 2018-06-27, Abdur-Rahmaan Janhangeer wrote: > what is more recommended and why? using configparser for settings or import > variables from file? Importing variables from a file is dangerous because it can execute arbitrary code. It should never be done with files provided by the user. Using configparser is far, far safer. -- Grant Edwards grant.b.edwardsYow! Psychoanalysis?? at I thought this was a nude gmail.comrap session!!! -- https://mail.python.org/mailman/listinfo/python-list