Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #96028 > unrolled thread
| Started by | tdev@freenet.de |
|---|---|
| First post | 2015-09-04 12:11 -0700 |
| Last post | 2015-09-06 18:19 -0600 |
| Articles | 14 — 9 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Python handles globals badly. tdev@freenet.de - 2015-09-04 12:11 -0700
Re: Python handles globals badly. Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-04 13:48 -0600
Re: Python handles globals badly. Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-04 13:52 -0600
Re: Python handles globals badly. Chris Angelico <rosuav@gmail.com> - 2015-09-05 10:27 +1000
Re: Python handles globals badly. Michael Torrie <torriem@gmail.com> - 2015-09-04 19:42 -0600
Re: Python handles globals badly. Chris Angelico <rosuav@gmail.com> - 2015-09-05 11:54 +1000
Program in or into (was Python handles globals badly) Rustom Mody <rustompmody@gmail.com> - 2015-09-04 20:18 -0700
Re: Program in or into (was Python handles globals badly) Chris Angelico <rosuav@gmail.com> - 2015-09-05 13:31 +1000
Re: Program in or into (was Python handles globals badly) Steven D'Aprano <steve@pearwood.info> - 2015-09-06 12:35 +1000
Re: Program in or into (was Python handles globals badly) MRAB <python@mrabarnett.plus.com> - 2015-09-06 03:54 +0100
Re: Program in or into (was Python handles globals badly) Rustom Mody <rustompmody@gmail.com> - 2015-09-05 21:35 -0700
Re: Program in or into (was Python handles globals badly) random832@fastmail.us - 2015-09-06 01:26 -0400
Re: Program in or into (was Python handles globals badly) wxjmfauth@gmail.com - 2015-09-06 00:42 -0700
Re: Program in or into (was Python handles globals badly) Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-06 18:19 -0600
| From | tdev@freenet.de |
|---|---|
| Date | 2015-09-04 12:11 -0700 |
| Subject | Re: Python handles globals badly. |
| Message-ID | <58e0d1b5-a7ca-4811-9926-fba1b7ede83f@googlegroups.com> |
Now I want reflecting the latest answers:
I think mostly everything is said.
Maybe you skip directly to my conclusion on the end.
--
Nevertheless I want grasp some points out
where I think I could give a respective answer:
"You are shifting declaration from one to another place"
I would not say so:
maybe it is a special prefix, but that you write once
and not in every function (with additonal at least one "ugly" line of code).
See no shifting here.
And I think it cannot be duplicated elswhere cause you are in
your single module's namespace.
See no conflicts here.
"You would create than all in one scope"
(or "You would create namespace vulnerabilities")
I would not say so:
An enclosed code block has still its own scope.
But if there is a global module var (e.g. recognized by a distinct name)
inside this block than this var is from scope global.
See no conflicts here.
"Learn better Python"
I am sure I am not alone (although the thread seems to isolate my opinion)
And you maybe have recognized I have accepted that there is a need for "global".
But admit I have to chew hard.
See my conclusion.
"Java have it ugly too"
I would not say so:
The examples providing with one var and one function does
not reflecting real scenarios.
Guess mostly classes have about 10 members and 10 functions.
Each member about 10 chars long with 10 lines of code per function.
seeing the global var inline is more readable than
looking on the top of the function if the var is global or not
+ the addtional lines of code onyl cause making it global.
I dislike @xxxx too (also in Java).
See the contamination in that point still in Python.
--
Conclusion:
-----------
I think we can agree
- that there are (unfortunately) low-level issues
that makes the removing of "global" impossible. !?
- and there are also special (dynamic) programming constructs
where a keyword "global" makes sense and should be available !?
I think for a scripting language this would be a good solution:
when used then the compiler should definitive look in the global namespace,
otherwise the compiler has some "magic" to distinct global from local
(probably simply by varname and scope of birth)
And yes it is not that I want change Python mantras.
But Python philospy is so far I know:
"There's a most understandable way to do something and that is how it should be done"
But that says not: Do not change things that makes the life easier for a developer
(escpecially who wants to do simple or powerful procedural/functional scripting).
So for me there are 6 things that is somehow "masochistic" in Python
cause with all its powerful features it is remarkable that it is not addable.
So, If I had the power I would include following features
(especially as we talk about scripting and cause why not (?),
it would not harm anyone who does not want to use it (!)):
all is optional (so hardcore Pythonier would probabyl deny to make use of it):
1. optional keyword "global" (if technical possible)
2. switch statement
3. less restrictive indentation
(as if it would really matter forgetting an empty space somewhere
if something is intended more right than is the context clear for me)
4. universal scope
5. goto label
6- "include" script statement (extending namespace to another script, like PHP)
I think this would make Python even more popular than it is now.
Not to say: I would really love it then.
Last but not least:
Why does javascript, as sripting language too, need not such things like
a keyword "global". It also very powerful language with dynamic binding, OO, ...
This is my question now.
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-04 13:48 -0600 |
| Message-ID | <mailman.146.1441396163.8327.python-list@python.org> |
| In reply to | #96028 |
On Fri, Sep 4, 2015 at 1:11 PM, <tdev@freenet.de> wrote:
> 6- "include" script statement (extending namespace to another script, like PHP)
def include(filename):
exec(open(filename).read())
> Last but not least:
> Why does javascript, as sripting language too, need not such things like
> a keyword "global". It also very powerful language with dynamic binding, OO, ...
I thought you didn't want to talk about comparisons to other languages. :-P
Javascript uses the same stupid system that Lua does, where
*everything* is automatically global/nonlocal unless you specifically
declare it as local.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-04 13:52 -0600 |
| Message-ID | <mailman.147.1441396389.8327.python-list@python.org> |
| In reply to | #96028 |
On Fri, Sep 4, 2015 at 1:48 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Sep 4, 2015 at 1:11 PM, <tdev@freenet.de> wrote:
>> 6- "include" script statement (extending namespace to another script, like PHP)
>
> def include(filename):
> exec(open(filename).read())
Sorry, that doesn't work because it takes locals from the include
function. You probably need something more like this:
def include(filename, globals):
exec(open(filename).read(), globals)
To be called like:
include("foo.py", globals())
If you want to get fancy you could probably have include inspect the
stack to pull the globals from the parent stack frame instead of
explicitly passing them in.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-09-05 10:27 +1000 |
| Message-ID | <mailman.151.1441412835.8327.python-list@python.org> |
| In reply to | #96028 |
On Sat, Sep 5, 2015 at 5:11 AM, <tdev@freenet.de> wrote:
> 1. optional keyword "global" (if technical possible)
As we've been saying in this thread, the problem isn't the
technicalities of implementation, but the ambiguity of syntax. To
eliminate the global statement, you need to either (a) declare all
your globals at module level, the way C-like languages do, or (b)
adorn every usage with either a magic naming convention or a
dot-lookup.
If you want the first one, well, there are languages like that, and
you're welcome to use those. For the latter, it's easy enough to do
something like this:
import types
_g = types.SimpleNamespace()
def accumulate(x):
_g.accum += x
return _g.accum
Look, Ma! No global statement!
> 2. switch statement
Check out the suggestions in PEP 3103. There are some plausible
options, and the known problems with them.
> 3. less restrictive indentation
> (as if it would really matter forgetting an empty space somewhere
> if something is intended more right than is the context clear for me)
In the face of ambiguity, refuse the temptation to guess. Can you
imagine the horrors if Python started guessing at indentation?
count = 0
for file in files:
for line in open(file):
process(line)
count += 1
It's obvious that process(line) goes inside the nested loop. What
about the counter? Literally the only difference between "count the
lines processed" and "count the files processed" is the indentation of
one line. You could make these kinds of things unambiguous, but only
by adding some other rule, like "always have a blank line at the end
of a loop", which would be just as restrictive.
> 4. universal scope
You can inject stuff into the built-ins, is that good enough?
> 5. goto label
Already exists! http://entrian.com/goto/
> 6- "include" script statement (extending namespace to another script, like PHP)
Ugh. There are a number of ways you can describe the semantics of
"include", and you picked what is, in my opinion, the very worst.
PHP's include operation switches back to HTML mode, which isn't itself
unreasonable, but it highlights the fact that "starting in HTML mode"
is stupid for any system of the size and complexity to want include();
but notably, it is *not* a simple text inclusion. You can include at
top level, and that works fine (I think). You can include inside a
function, and that mostly works (but it's not the same as dumping the
source code in at that point). But you can't, for some bizarre reason,
include inside a class definition. Not at all.
Fortunately, it's easy enough to preprocess source code. And if you
want that functionality in Python, that's probably the best way to do
it, too. The only special I can think of would be to indent the file
to the exact level that the #include directive is indented. (And yes,
I think using "#include" in Python would be correct; that way, syntax
highlighters read it as a comment.)
Most of what you want can be done already. If you want the language to
grow these features, you need to explain how the feature is different
from what can already be done.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2015-09-04 19:42 -0600 |
| Message-ID | <mailman.154.1441417841.8327.python-list@python.org> |
| In reply to | #96028 |
On 09/04/2015 06:27 PM, Chris Angelico wrote: > If you want the first one, well, there are languages like that, and > you're welcome to use those. For the latter, it's easy enough to do > something like this: > > import types > _g = types.SimpleNamespace() > > def accumulate(x): > _g.accum += x > return _g.accum > > Look, Ma! No global statement! Since most of the time for me when I need a global, I need it to be an app global (more than just one module) and I use it to store configuration. So I just use another module for that. import my_global_module as _g _g.some_setting = 5 I get the impression, thought, that our esteemed poster is still trying to battle the Java windmill, but in Python now and will never accept anything we try to tell him about the Python way. Python does have its warts, but often attempts to fix the warts would just make things a lot worse. So I accept them as part of Python's character and try to use them to my advantage.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-09-05 11:54 +1000 |
| Message-ID | <mailman.155.1441418073.8327.python-list@python.org> |
| In reply to | #96028 |
On Sat, Sep 5, 2015 at 11:42 AM, Michael Torrie <torriem@gmail.com> wrote: > On 09/04/2015 06:27 PM, Chris Angelico wrote: >> If you want the first one, well, there are languages like that, and >> you're welcome to use those. For the latter, it's easy enough to do >> something like this: >> >> import types >> _g = types.SimpleNamespace() >> >> def accumulate(x): >> _g.accum += x >> return _g.accum >> >> Look, Ma! No global statement! > > Since most of the time for me when I need a global, I need it to be an > app global (more than just one module) and I use it to store > configuration. So I just use another module for that. > > import my_global_module as _g > > _g.some_setting = 5 Yeah. Comes to the same thing; if you use a dotted lookup, it's not assigning to the global. Of course, you still have all the other concerns about globals. You've just buried them behind a level of indirection. > I get the impression, thought, that our esteemed poster is still trying > to battle the Java windmill, but in Python now and will never accept > anything we try to tell him about the Python way. Python does have its > warts, but often attempts to fix the warts would just make things a lot > worse. So I accept them as part of Python's character and try to use > them to my advantage. Indeed. The key to being a good programmer is not "write your code despite the language you're using", but "write the code in the language you're using". ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2015-09-04 20:18 -0700 |
| Subject | Program in or into (was Python handles globals badly) |
| Message-ID | <03c53b53-4103-4fd8-8851-caeccda8e016@googlegroups.com> |
| In reply to | #96038 |
On Saturday, September 5, 2015 at 7:24:47 AM UTC+5:30, Chris Angelico wrote: > Indeed. The key to being a good programmer is not "write your code > despite the language you're using", but "write the code in the > language you're using". > A thought experiment for you Chris! Here's mergesort written in various languages http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort You could look at the java if you like but I think C# takes the cake. And of course also there's the python Now the thought experiment: For some reason you need to code in C# [You need to do this part of the experiment honestly!!] Would you write the C# code? Or would you write the python-ish code in C# ?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-09-05 13:31 +1000 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <mailman.157.1441423916.8327.python-list@python.org> |
| In reply to | #96042 |
On Sat, Sep 5, 2015 at 1:18 PM, Rustom Mody <rustompmody@gmail.com> wrote: > Now the thought experiment: > > For some reason you need to code in C# > [You need to do this part of the experiment honestly!!] > > Would you write the C# code? > Or would you write the python-ish code in C# ? As I'm not familiar with C#, I'd probably end up writing Python-style code in C#. But I wouldn't then complain that C# isn't sufficiently like Python; if I want Python, I know where to find it. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-09-06 12:35 +1000 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <55eba65d$0$1665$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #96042 |
On Sat, 5 Sep 2015 01:18 pm, Rustom Mody wrote: > Here's mergesort written in various languages > http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort > > You could look at the java if you like but I think C# takes the cake. > And of course also there's the python > > Now the thought experiment: > > For some reason you need to code in C# > [You need to do this part of the experiment honestly!!] > > Would you write the C# code? > Or would you write the python-ish code in C# ? That depends. Is the example C# code idiomatic for the language? Or was it written by somebody ignorant of C#, and consequently is a poor example of badly-written and unidiomatic "Java in C#"? If the first, then I expect I would write the C# code, because it is idiomatic and works. What would you do? It is certainly true that C# appears to be a more verbose language than Python. Based on this example, it prefers to use classes with methods rather than stand-alone functions, it requires static declarations, there's a lot of boilerplate needed to get things to work. It seems to lack some nice semantic features of Python, such as list slices and the ability to treat lists/arrays as first class values, that make Python so easy to work with. If C# lacks those features, how do you expect to use them? Lacking some of those features (or at least having significant downsides to the use of them) is why idiomatic C# or Java code looks the way it does. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-09-06 03:54 +0100 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <mailman.164.1441508106.8327.python-list@python.org> |
| In reply to | #96049 |
On 2015-09-06 03:35, Steven D'Aprano wrote: > On Sat, 5 Sep 2015 01:18 pm, Rustom Mody wrote: > >> Here's mergesort written in various languages >> http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort >> >> You could look at the java if you like but I think C# takes the cake. >> And of course also there's the python >> >> Now the thought experiment: >> >> For some reason you need to code in C# >> [You need to do this part of the experiment honestly!!] >> >> Would you write the C# code? >> Or would you write the python-ish code in C# ? > > > That depends. Is the example C# code idiomatic for the language? Or was it > written by somebody ignorant of C#, and consequently is a poor example of > badly-written and unidiomatic "Java in C#"? > > If the first, then I expect I would write the C# code, because it is > idiomatic and works. What would you do? > > It is certainly true that C# appears to be a more verbose language than > Python. Based on this example, it prefers to use classes with methods > rather than stand-alone functions, it requires static declarations, there's > a lot of boilerplate needed to get things to work. It seems to lack some > nice semantic features of Python, such as list slices and the ability to > treat lists/arrays as first class values, that make Python so easy to work > with. If C# lacks those features, how do you expect to use them? > > Lacking some of those features (or at least having significant downsides to > the use of them) is why idiomatic C# or Java code looks the way it does. > C# and Java don't have functions. The closest you can get is static (class) methods. Note: C programs start with the "main" function. Java tries to follow that pattern, except that it doesn't have functions, so a static method "main" is used instead, which means that it also requires a class to hold it. Similarly, C has "sin" and "cos" functions. Java tries to follow that pattern, except that it doesn't have functions, so a static methods are used instead, which means that it also requires a class to hold them. And C# follows what Java does.
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2015-09-05 21:35 -0700 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <19e2222b-83d8-450a-9943-aab0083cd101@googlegroups.com> |
| In reply to | #96049 |
On Sunday, September 6, 2015 at 8:05:28 AM UTC+5:30, Steven D'Aprano wrote: > On Sat, 5 Sep 2015 01:18 pm, Rustom Mody wrote: > > > Here's mergesort written in various languages > > http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort > > > > You could look at the java if you like but I think C# takes the cake. > > And of course also there's the python > > > > Now the thought experiment: > > > > For some reason you need to code in C# > > [You need to do this part of the experiment honestly!!] > > > > Would you write the C# code? > > Or would you write the python-ish code in C# ? > > > That depends. Is the example C# code idiomatic for the language? Or was it > written by somebody ignorant of C#, and consequently is a poor example of > badly-written and unidiomatic "Java in C#"? > > If the first, then I expect I would write the C# code, because it is > idiomatic and works. What would you do? I'd cringe And suspect malefide intent on the part of those who wrote that code to show C# in bad light :-) Thats partly joke because the java looks somewhat better and C# and Java are not very different My main point of course was that between the two extremes: 1. What the language mandates eg python mandates a ':' after the if condition C mandates that it be parenthesized 2. Matters of free personal taste there is a large in-between grey area where the language's 'keepers' recommend best (and not) practices which may not exactly be the best for a programmer. eg Consider perl and python. As for regex support they are mostly the same Yet a typical pythonista takes pride that he wrote a 10 line re-free function where a 2-line re would have sufficed A typical perl guy will take pleasure in exactly the opposite achievement A programmer wanting to get his job done need not believe any of these fads&fancies
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2015-09-06 01:26 -0400 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <mailman.165.1441517206.8327.python-list@python.org> |
| In reply to | #96049 |
Steven D'Aprano <steve@pearwood.info> writes: > That depends. Is the example C# code idiomatic for the language? Not in the least. My first clue was Int32 - nobody actually uses those names. > Or was it > written by somebody ignorant of C#, and consequently is a poor example of > badly-written and unidiomatic "Java in C#"? > > If the first, then I expect I would write the C# code, because it is > idiomatic and works. What would you do? > > It is certainly true that C# appears to be a more verbose language than > Python. Based on this example, it prefers to use classes with methods > rather than stand-alone functions, You certainly need to put methods in a class, but it's absurd that they made it an actual instantiable class with instance properties for the variables they required for sorting, rather than there being a class that's just there as a container for the functions. I wouldn't do that in Java, either. They also gave it features a lot of the shorter versions on the page don't have; the ability to fall back to insertion sort for short lists, and the ability to tune exactly how it does that, and how many subsequences to sort and merge.
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2015-09-06 00:42 -0700 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <1c356437-1f9d-4c3c-996c-19ffe5b05adf@googlegroups.com> |
| In reply to | #96052 |
------ Unicode: Contrary to this *disastrous* Python, C# is excellent. The best document I ever seen on the web explaining Unicode was written with C# and pdfcsharp. (> 350 A4 pages, on my hd).
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-06 18:19 -0600 |
| Subject | Re: Program in or into (was Python handles globals badly) |
| Message-ID | <mailman.189.1441585230.8327.python-list@python.org> |
| In reply to | #96049 |
On Sat, Sep 5, 2015 at 8:54 PM, MRAB <python@mrabarnett.plus.com> wrote: > And C# follows what Java does. Except where the language designers recognized that the Java way was poorly conceived or implemented and did it better. Generally speaking, I would much prefer to work in C# over Java, if only the language weren't so closely tied to .NET.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web