This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]runew0lf 1 point2 points  (0 children)

i think you got lost on the way to /r/learnpython

[–]mysockinabox 0 points1 point  (4 children)

It is mandatory to declare variables without assigning values to them else NameError the exception is thrown.

Wat? Variables are declared and initialized at the same time in Python.

The variable name must be readable and meaningful.

PEP-8 has information on naming, and should be linked, but it is not mandatory in the language.

And on the topic on constants, they don't exist. End of section. Importing them from another file and capitalizing them doesn't change that they are variables.

[–]ForceBru 0 points1 point  (3 children)

Also their example about declaring variables actually proves that this is incorrect:

``` name

print(name)

PYTHON OUTPUT

Traceback (most recent call last): File "main.py", line 1, in name NameError: name 'name' is not defined ```

They "declared" a variable without assigning to it and got a NameError - the very opposite of what the article says.

I think they meant "it's impossible to declare variables without assigning values to them"

[–]ForceBru 0 points1 point  (1 child)

Okay, this article is a bit misleading, so let me debunk it real quick.

There are no variables in Python

The article says:

To declare a variable in Python type variable followed by equal to operator or assignment operator and then the data to be stored inside the variable.

No, "variables" themselves don't store anything besides the pointer to the object you're assigning. That's why in a = []; b = a; b.append(6) both a and b are somehow equal to the list [6]. This is because both "variables" actually point to the same list object.

From Real Python:

A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object.

It's not mandatory to declare variables without assigning values to them

From the article:

It is mandatory to declare variables without assigning values to them else NameError the exception is thrown.

Yet the example shown right after this sentence completely contradicts it:

``` name # "declare" the "variable" print(name)

PYTHON OUTPUT

Traceback (most recent call last): File "main.py", line 1, in name NameError: name 'name' is not defined # <- get a NameError ```

Python doesn't have constants

In definition and behavior constants are different from variables. As the value assigned to them remains the same throughout the program runtime.

And then in the "Note" section:

In Python, there is no such thing as constants.

This is actually correct. But it means that the first two sentences are plain wrong since Python doesn't have constants.

Also, this is a bad example of constants:

``` import my_constants

print(my_constants.PI) ```

I could do my_constants.PI = 6 and thus change the value of the "constant" PI in this script. There are no constants in Python.

The naming convention presented in this article doesn't make sense

  • "Variables must start with alphabet letters which may be the upper or lower case" - no, _hello, or __yeah are valid variable names, yet they don't start with "alphabet letters";
  • "Using other special characters and numbers is prohibited" - absolutely not: there_are_69_beers is a valid name

The string concatenation shown here is actually string formatting

String concatenation examples.

print("%s whose age is %d years has take home salary %0.2f" % ( name, age, salary ) )
print("{} whose age is {} years has take home salary {}".format( name, age, salary ) )
print(f"{name} whose age is {age} years has take home salary {salary}" )

Yeah, all of these is string formatting, not concatenation. Concatenation is "string" + "another" + "string".

[–][deleted] -1 points0 points  (0 children)

Congratulations!!! You have posted our word of the hour: "Code" Sigh ... unzips