Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Sedro
Dec 31, 2008

Dominoes posted:

Speaking of which:

What's the proper way to handle this?
You got your answer but FYI those are just style warnings in your IDE. It's also suggesting you add semicolons.

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

seiken posted:

Python code:
> x = 1
> print [x for x in xrange(0, 4)]
[0, 1, 2, 3]
> print x
3
> def f(): print x
> f()
3
> def f(): print x; x = 1
> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
UnboundLocalError: local variable 'x' referenced before assignment
turns out having no proper scopes other than function and no variable declaration means everything is hack to make anything behave sensibly, who would have thought

I see explicit proper scoping here; x starts off in a global scope, and then you create a function that declares a variable "x" in a local scope. Since x now belongs to a local scope, trying to print that variable before assigning it throws an error. If you don't push a variable "x" into the local scope, then Python knows that you're still talking about the global x. If you want the function to use the global x, then you can use the "global" keyword for that, which tells the function that x belongs to a different scope. Totally reasonable, and definitely not broken

Maybe what you expected was for Python to interpret line by line instead of interpreting the entire function block before running it?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
The hosed up thing in that python example is the name used in the list comprehension leaking out to the code outside of it. Everything else seems fine.

seiken
Feb 7, 2005

hah ha ha

QuarkJets posted:

Maybe what you expected was for Python to interpret line by line instead of interpreting the entire function block before running it?

no, I expected Python to do what it does, because I know exactly how it works. It's still obtuse for name lookup semantics to change based on assignments arbitrarily far after the lookup. I mean it's a reasonable decision given that you've already made the mistake of conflating variable assignment and declaration, but that doesn't mean it's not poo poo. And you ignored the name leaking out of the comprehension because of the lack of any sort of block scoping

seiken fucked around with this message at 03:21 on Dec 7, 2014

QuarkJets
Sep 8, 2008

Jabor posted:

The hosed up thing in that python example is the name used in the list comprehension leaking out to the code outside of it. Everything else seems fine.

Why is that a problem? A list comprehension is basically just a type of for loop, so it's the same behavior that you'd get from any other language with for loops

seiken
Feb 7, 2005

hah ha ha

QuarkJets posted:

Why is that a problem? A list comprehension is basically just a type of for loop, so it's the same behavior that you'd get from any other language with for loops

Every sane language with for loops gives them their own lexical scope.

vvvv since your post doesn't make sense in response to mine, this is what I mean by "arbitrarily"

Python code:
def f():
  # the semantics of this statement
  print x

  # ... stuff happens ...

  # ... more code ...

  # ... and  so on ...

  # is changed by this statement, which is arbitrarily far in the future
  x = 1

seiken fucked around with this message at 03:29 on Dec 7, 2014

QuarkJets
Sep 8, 2008

^^^ So you're saying that C, C++, and Java, all of which would produce identical behavior, are insane?

seiken posted:

no, I expected Python to do what it does, because I know exactly how it works. It's still obtuse for name lookup semantics to change based on assignments arbitrarily far after the lookup. I mean it's a reasonable decision given that you've already made the mistake of conflating variable assignment and declaration, but that doesn't mean it's not poo poo. And you ignored the name leaking out of the comprehension because of the lack of any sort of non-function scoping

It's not arbitrary, though. When you declare a variable within a new scope, you're implicitly assigning that variable name within that scope to the new scope.

QuarkJets
Sep 8, 2008

IE

C++ code:
int i;
for(i=0; i < 5; i++) {
  cout << i << endl;
}

cout << i << endl;
}
The result is 5, and that's not surprising

seiken
Feb 7, 2005

hah ha ha

QuarkJets posted:

^^^ So you're saying that C, C++, and Java, all of which would produce identical behavior, are insane?

Have you ever actually used any of them? None of them work like that

C++ code:
#include <iostream>
 
int main() {
	for (int i = 0; i < 10; ++i);
	std::cout << i;
}
code:
prog.cpp: In function ‘int main()’:
prog.cpp:5:15: error: ‘i’ was not declared in this scope
  std::cout << i;

QuarkJets
Sep 8, 2008

seiken posted:

Have you ever actually used any of them? None of them work like that

C++ code:
#include <iostream>
 
int main() {
	for (int i = 0; i < 10; ++i);
	std::cout << i;
}
code:
prog.cpp: In function ‘int main()’:
prog.cpp:5:15: error: ‘i’ was not declared in this scope
  std::cout << i;

Yes, I have. Have you? Copy-pasting that code and compiling it, it prints 10. I just did it, using the g++ (GCC) 4.4.7 compiler.

e: Admittedly, I've never tried doing this in Java, but in C++ it's permitted

QuarkJets fucked around with this message at 03:41 on Dec 7, 2014

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
With g++ 4.8.1 I get:

code:
main.cpp: In function 'int main()':
main.cpp:5:18: error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
     std::cout << i;
                  ^
main.cpp:5:18: note: (if you use '-fpermissive' G++ will accept your code)
make: *** [main.o] Error 1
I've never actually run g++ with -fpermissive, and if it will allow me to have ambiguous scopes like that I never will. Specifically with C++ I want as much help from the compiler as possible. Restrictions are there to help you not gently caress up.

Joda fucked around with this message at 03:51 on Dec 7, 2014

raminasi
Jan 25, 2005

a last drink with no ice

QuarkJets posted:

Yes, I have. Have you? Copy-pasting that code and compiling it, it prints 10. I just did it, using the g++ (GCC) 4.4.7 compiler.

e: Admittedly, I've never tried doing this in Java, but in C++ it's permitted

That is non-standard behavior and I'm surprised any version of g++ does it, given how much poo poo Microsoft caught for providing the same extension.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

QuarkJets posted:

Yes, I have. Have you? Copy-pasting that code and compiling it, it prints 10. I just did it, using the g++ (GCC) 4.4.7 compiler.

e: Admittedly, I've never tried doing this in Java, but in C++ it's permitted

Not in anything standards-compliant it's not. GCC may ignore parts of the spec when you don't explicitly tell it to do it right, but using that behaviour to argue that something's allowed by the language is wrong.

Coffee Mugshot
Jun 26, 2010

by Lowtax

QuarkJets posted:

Yes, I have. Have you? Copy-pasting that code and compiling it, it prints 10. I just did it, using the g++ (GCC) 4.4.7 compiler.

uhhhhh

code:
$ cat main.cpp

#include <iostream>
 
int main() {
  for (int i = 0; i < 10; ++i);
  std::cout << i;
}
code:
$ g++-4.4 -v main.cpp

GNU C++ (Ubuntu/Linaro 4.4.7-8ubuntu1) version 4.4.7 (x86_64-linux-gnu)
	compiled by GNU C version 4.4.7, GMP version 5.1.3, MPFR version 3.1.2-p3.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 0ae2a6eb43bbfaa51b9b80e6859c4ec1
main.cpp: In function ‘int main()’:
main.cpp:5: error: name lookup of ‘i’ changed for ISO ‘for’ scoping
main.cpp:5: note: (if you use ‘-fpermissive’ G++ will accept your code)
EDIT: Please don't troll me ITT. Do not compile your C++ code with -fpermissive and then pretend it's fine when it just gives you warning.

https://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/C_002b_002b-Dialect-Options.html#index-fpermissive-140 posted:

Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile.

Coffee Mugshot fucked around with this message at 03:51 on Dec 7, 2014

fritz
Jul 26, 2003

Joda posted:


I've never actually run g++ with -fpermissive, and if it will allow me to have ambiguous scopes like that I never will. Specifically with C++ I want as much help from the compiler as possible. They're there to help you not gently caress up.

When I started at this company I got put on a project with a guy who used -fpermissive and I had to spend a good solid week going through his stuff and fixing it up to compile without it, and on clang++.

Don't use -fpermissive.

fritz
Jul 26, 2003

QuarkJets posted:

Yes, I have. Have you? Copy-pasting that code and compiling it, it prints 10. I just did it, using the g++ (GCC) 4.4.7 compiler.

e: Admittedly, I've never tried doing this in Java, but in C++ it's permitted

Wait what the hell are you doing on 4.4.7, that's closing in on three years old.

fritz
Jul 26, 2003

QuarkJets posted:

e: Admittedly, I've never tried doing this in Java


code:
  for (int i = 0; i < 10; i++) {

        }
        int j = i;
gives
code:
Error:(39, 17) error: cannot find symbol variable i

Coffee Mugshot
Jun 26, 2010

by Lowtax
Let me personally assure you that gcc has never and will never accept that nonconformant code without error.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Thermopyle posted:

Maybe Python has stockholmed me, but that seems perfectly reasonable to me.

Yeah, Python has stockholmed me. I'm just so used to that behavior it just seems normal.

seiken
Feb 7, 2005

hah ha ha
Also note that this is only even an issue for the specific case of a variable declared in the loop preamble - if the code was

C++ code:

#include <iostream>

int main() {
  for (;;) int i = 0;
  std::cout << i;
}

Then not even GCC with -fpermissive or any buggy version of MSVC will accept it

OddObserver
Apr 3, 2009

Voted Worst Mom posted:

Let me personally assure you that gcc has never and will never accept that nonconformant code without error.

Are you really sure that something like 2.7 or somesuch prehistoric version didn't? ;-)

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
If you're writing scripts to automate some mundane task, feel free to use Python- if it's only a few dozen lines you could use any language under the sun and it wouldn't matter. Don't use it to teach beginners. It's a hodgepodge of half-baked ideas and corner cases. Students come away with confusion, weak mental models and bad habits. I think many developers who learned Python as a third or fourth language and found it easy to pick up fail to objectively evaluate its complexity from the perspective of someone who has no prior experience.

While I'm in a ranting mood, let's take a look at Python's stated aesthetic principles.

import this posted:

Explicit is better than implicit.
Then why not:
  • declare and initialize variables before use?
  • annotate variables and procedure arguments with explicit type information?
  • declare and implement explicit interfaces rather than avoiding them with duck-typing?

import this posted:

Sparse is better than dense.
Debatable.

import this posted:

Readability counts.
Readability is a treacherous argument. For example, consider the use of the word or as a logical inclusive-or. Is this more intuitive and readable than a symbol like |? An English speaker might be tempted to say yes, forgetting that when that word is used in everyday conversation (Would you like cake or pie for dessert?) the implied meaning is closer to a logical exclusive-or. Neither | nor or are inherently clearer to someone unfamiliar with the rules of the programming language.

import this posted:

Special cases aren't special enough to break the rules.
Then why does Python have special precedence rules for obscure cases like this?
Python code:
>>> x = 5
>>> 1 <= x < 99
True

import this posted:

Errors should never pass silently.
Unless explicitly silenced.
Sounds to me like this would imply the Python community should embrace checked exceptions.

import this posted:

There should be one-- and preferably only one --obvious way to do it.
Why does Python have over a dozen possible string literal variants when nearly every other mainstream language gets by with 1 or 2?
Python code:
>>> 'string'
'string'
>>> "string"
'string'
>>> '''string'''
'string'
>>> u'string'
u'string'
>>> r"string"
'string'
>>># and so on...

import this posted:

If the implementation is hard to explain, it's a bad idea.
https://www.python.org/download/releases/2.3/mro/

import this posted:

Namespaces are one honking great idea -- let's do more of those!
Then why not have actual block scope like most sane languages have had since ALGOL-60?

Internet Janitor fucked around with this message at 17:43 on Dec 7, 2014

a cyberpunk goose
May 21, 2007

Recovered python addict chiming in.

I recently was attempting to explain some python constructs to a programming newbie at work who is trying to get better so she can be a more valuable employee and all that fun jazz. I was able to cover a lot of ground in simple terms, just describing core types, iterables and for loops, all that jazz, but when it came time to explain classes is when I started to feel really embarrassed about python.

Trying to explain python classes to programming newbies is the worst if you think about it

"okay, so, in a lot other languages when you define a method in a class they will implicitly slip in a 'this' that you'll refer to talk to your own instance, but python is weird and when you have a class instance with methods you need to put 'self' at the beginning of your method, it also wont resolve what variables you're trying to reference, you will always need to rely on 'self' for anything"

it is clunky and awful to teach, because you dont want them to think all programming languages are doing the quirky things it does. a lot of pythonesque things are easy to conceptualize and describe -- to other programmers, for beginners it's a nightmare to teach and be taught.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
As someone who has never actually used or even looked at Python before, am I understanding it correctly that there is no clear syntactical difference between variable declaration, definition and assignment?

The Insect Court
Nov 22, 2012

by FactsAreUseless

Internet Janitor posted:

Why does Python have over a dozen possible string literal variants when nearly every other mainstream language gets by with 1 or 2?
Python code:
>>> 'string'
'string'
>>> "string"
'string'
>>> '''string'''
'string'
>>> u'string'
u'string'
>>> r"string"
'string'
>>># and so on...

An 'r' means that backslashes are treated like a normal part of the string instead of an escape character, 'b' is an array of bytes and is used mainly for interfacing with non-python or legacy code that doesn't use unicode. The "problem" is mostly cleared up by using Python 3, and the two modifier characters don't mean there are a "dozen string constructors". And dealing with unicode in Python is no more difficult(and generally easier) than in most other languages.


Mido posted:

"okay, so, in a lot other languages when you define a method in a class they will implicitly slip in a 'this' that you'll refer to talk to your own instance, but python is weird and when you have a class instance with methods you need to put 'self' at the beginning of your method, it also wont resolve what variables you're trying to reference, you will always need to rely on 'self' for anything"

it is clunky and awful to teach, because you dont want them to think all programming languages are doing the quirky things it does. a lot of pythonesque things are easy to conceptualize and describe -- to other programmers, for beginners it's a nightmare to teach and be taught.

"Methods are just functions, except when you call a method Python automatically passes as an additional argument the instance from which it's being called. Remember that when writing the parameter list for methods." You've got it backwards, it's implicit self languages like Java that complicate things by special-casing methods, which is possible because they're less dynamic and you can't add a method to a class at runtime as you can in Python.

Python's got ugly bits, but I don't think the two examples given make it a bad language for teaching. Especially relative to other language choices, I'm still not clear what languages are being proposed as a replacement.

ErIog
Jul 11, 2001

:nsacloud:

Mido posted:

Recovered python addict chiming in.

I recently was attempting to explain some python constructs to a programming newbie at work who is trying to get better so she can be a more valuable employee and all that fun jazz. I was able to cover a lot of ground in simple terms, just describing core types, iterables and for loops, all that jazz, but when it came time to explain classes is when I started to feel really embarrassed about python.

Trying to explain python classes to programming newbies is the worst if you think about it

"okay, so, in a lot other languages when you define a method in a class they will implicitly slip in a 'this' that you'll refer to talk to your own instance, but python is weird and when you have a class instance with methods you need to put 'self' at the beginning of your method, it also wont resolve what variables you're trying to reference, you will always need to rely on 'self' for anything"

it is clunky and awful to teach, because you dont want them to think all programming languages are doing the quirky things it does. a lot of pythonesque things are easy to conceptualize and describe -- to other programmers, for beginners it's a nightmare to teach and be taught.

I don't see why this behavior is bad. It seems like it would be helpful for beginners to understand that they're accessing something specific to that object instance instead of something that exists outside the object. It's just making the scoping of class instances explicit rather than implicit. You can make the case they're not following through on a lot of their design goals, but requiring 'self' when referring to data or methods belonging to the class instance doesn't seem like it violates any of their design goals. It doesn't even strike me as particularly annoying.

ErIog fucked around with this message at 08:10 on Dec 7, 2014

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Internet Janitor posted:

While I'm in a ranting mood, let's take a look at Python's stated aesthetic principles.

"I do not understand Python's features and syntax well enough, thus I should mock it"

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Joda posted:

As someone who has never actually used or even looked at Python before, am I understanding it correctly that there is no clear syntactical difference between variable declaration, definition and assignment?

Yes. You create a variable x in a function by assigning to x somewhere in that function. If x is declared in a function, all references to x resolve to the local variable, even if they occur lexically prior to the first assignment. This rule means that you can't just assign to a global variable from inside a function; you have to suppress the local variable by declaring that x is a global name, and then assignments will actually affect the global variable.

Dominoes
Sep 20, 2007

Lumpy posted:

Assuming java script:

code:
var i;
for (i in lats) {
...

for (i in longs) {
...

pseudorandom name posted:

Well, the proper way is:
code:
for (let lat_ of lats) {
but then you run into browser compatibility problems.

Sedro posted:

FYI those are just style warnings in your IDE. It's also suggesting you add semicolons.
Thanks dudes.

Dominoes fucked around with this message at 10:42 on Dec 7, 2014

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Voted Worst Mom posted:

Let me personally assure you that gcc has never and will never accept that nonconformant code without error.

Let me quote from the original, pre-standard, Annotated C++ Reference Manual (Ellis & Stroustrup):

quote:

6.5.3 The for Statement
[...]
If the for-init-statement is a declaration, the scope of the names declared extends to the end of the block enclosing the for-statement.

It goes on to comment:

quote:

[...] the same name cannot be used to control two for loops in the same scope.
code:
for (int i = 0; i<100; i++) {
  // ...
}
for (int i = 0; i<100; i++) { // error: `i' defined twice
  // ...
}

Every C++ compiler behaved this way before the standard came out.

canis minor
May 4, 2011

Joda posted:

But if you're gonna be a developer I assume you're not just gonna take a single programming course? Don't all CS, software engineering or software development B.Sc./M.Sc. programmes have algorithms and data structures (or some equivalent thereof) as a core course? I know that understanding how a CPU is wired (at least in terms of a basic integer unit) and how it interprets instructions is only consistently core for software engineering, but I thought everyone doing some variation of an academic degree had to take algorithms/datastructures at some point?

Yes - if you're going to be a developer. If you're not - then I don't think you're going to touch these subjects. During my studies (IT at the department of Mathematics and Computer Sciences) these things were also mandatory - I don't know if it's the same elsewhere (I'd assume so), but for example, in UK, I worked with people that knew the language, but not much beside it.

At my uni we began with a year full of mathematics - differential equations, linear algebra, discrete mathematics etc. To this day I don't know if these helped me, or if I just lost a year. Sure - I can gloat that, at that time, I knew how to calculate different types of probabilities or determine what is a group / core in a given set. Then we went through applications of mathematical subjects in Maple / Mathematica, with introduction to algorithms with Ada, and alongside, theoretical subjects like construction of compilers. C/C++ begun on my fourth semester, if I remember correctly, with data structures alongside that.

Deus Rex
Mar 5, 2005

Suspicious Dish posted:

"I do not understand Python's features and syntax well enough, thus I should mock it"

"I cannot rebut reasonable criticism of my pet programming language"

SupSuper
Apr 8, 2009

At the Heart of the city is an Alien horror, so vile and so powerful that not even death can claim it.

Thermopyle posted:

Yeah, Python has stockholmed me. I'm just so used to that behavior it just seems normal.
Don't worry, every programmer has Stockholm Syndrome one way or another. We slave away for our computer overlords. :)

Soricidus
Oct 21, 2010
freedom-hating statist shill
guys, guys, this language that I am intimately familiar with behaves the way I expect, so clearly what it does is natural and logical and anyone who thinks it's flawed or inconsistent simply doesn't get it

Dominoes
Sep 20, 2007

Speaking of which, Python should fix the datetime module (Look at arrow), and include requests as the standard http module. Uncontroversial?

NFX
Jun 2, 2008

Fun Shoe

Why does this man have a phobia of scrolling?

fritz
Jul 26, 2003

NFX posted:

Why does this man have a phobia of scrolling?

Sitting alone in front of a computer screen all day for twenty years does weird things to a persons brain.

seiken
Feb 7, 2005

hah ha ha

Suspicious Dish posted:

"I do not understand Python's features and syntax well enough, thus I should mock it"

:allears:

I'm completely familiar with its features and syntax (and I know InternetJanitor is as well).

It's an inconsistent, backwards mess of a language that doesn't align in any way with its stated goals. What Python is good at is being a slightly less terrible bash, for couple-hundred line scripts. It's only managed to sneak under the radar of universally reviled languages because its awfulness is a bit more subtle than PHP or Javascript, but it's no less systemic. (and nobody has written a really good blog post a la "fractal of bad design")

Dominoes
Sep 20, 2007

seiken posted:

:allears:

I'm completely familiar with its features and syntax (and I know InternetJanitor is as well).

It's an inconsistent, backwards mess of a language that doesn't align in any way with its stated goals. What Python is good at is being a slightly less terrible bash, for couple-hundred line scripts. It's only managed to sneak under the radar of universally reviled languages because its awfulness is a bit more subtle than PHP or Javascript, but it's no less systemic. (and nobody has written a really good blog post a la "fractal of bad design")
Too far bro.

Adbot
ADBOT LOVES YOU

Pie Colony
Dec 8, 2006
I AM SUCH A FUCKUP THAT I CAN'T EVEN POST IN AN E/N THREAD I STARTED

seiken posted:

:allears:

I'm completely familiar with its features and syntax (and I know InternetJanitor is as well).

It's an inconsistent, backwards mess of a language that doesn't align in any way with its stated goals. What Python is good at is being a slightly less terrible bash, for couple-hundred line scripts. It's only managed to sneak under the radar of universally reviled languages because its awfulness is a bit more subtle than PHP or Javascript, but it's no less systemic. (and nobody has written a really good blog post a la "fractal of bad design")

This isn't even remotely true

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply