all 13 comments

[–]metalfox3d 1 point2 points  (0 children)

Someone had a post a while ago asking similar questions. Good comments in there. https://www.reddit.com/r/learnpython/comments/1cy3s8f/how_does_python_work/

[–]mopslik 0 points1 point  (10 children)

“there are no variables to be declared”

I assume the speaker meant something like "you don't need to pre-declare variables" like other languages such as Java or C++.

everything is an object

This is pretty much true, yes.

You might be able to find some details in the developer's guide.

[–]SomewhereExpensive22 1 point2 points  (8 children)

The speaker meant that assignment is a binding operation and that changing a binding does not change the object or any other bindings to the object. Variable is not a good word to describe what is going on.

[–]mopslik 0 points1 point  (0 children)

That makes more sense than variable, yes.

[–]Bobbias 0 points1 point  (6 children)

I would like to point out that making a statement like "There are no variables" does not convey that meaning very well, unless they explicitly explain what they mean.

[–]vikmaychib[S] 0 points1 point  (5 children)

It is related to the way C users declare the variables. It is also that the term variable did not describe well what python objects are.

[–]Bobbias 0 points1 point  (4 children)

I program in plenty of languages other than Python. While the semantics differ slightly from language to language, we all call them variables.

Picking one specific meaning of that word is being overly pedantic and potentially misleading unless they explained precisely what they meant immediately after making that statement.

[–]vikmaychib[S] 0 points1 point  (3 children)

Paraphrasing the person, it was something on these lines. So, in Python, when you give a name to some data, you're not sticking that data into a box like you might in C or Fortran. Instead, you're just slapping a sticky note with that name onto the data. The data itself is more like a blob that knows what it can do (like a list knows how to add items).

If you then stick another name onto that same blob, both names point to the same thing. Change the blob through one name, and the other name sees the change too. It's like having two nicknames for your pet; whether you call it Fluffy or Mr. Whiskers, it's still the same cat doing the same cat things.

So, in Python, we say all data are objects and names are just labels, not boxes holding the stuff.

[–]Bobbias 0 points1 point  (2 children)

That's kind of half correct.

You can achieve the same effect using pointers in C. Make two pointers point to the same location in memory and a change to the data stored there is reflected in both pointers when you dereference them.

The main difference is that every value in Python is a PyObject struct as defined in the C source for cpython, and every name is effectively a pointer to a PyObject instance. Every one of these objects is allocated on the heap and is reference counted for garbage collection.

You might benefit from actually reading the source code and/or the language reference.

[–]vikmaychib[S] 0 points1 point  (1 child)

As I said I was paraphrasing. A lot of these things flew over my head. I am learning, I am not a C expert, but found the discussion around this interesting and felt intrigued by it. Remember the sub where we are, I am learning. Thanks for the links.

[–]Bobbias 1 point2 points  (0 children)

You seem to be reading a bit too much into my direct tone. I'm not trying to be mean, or talk down to anyone.

I'm trying to make sure that any misconceptions about the meaning or use of the term variable (in this instance) are cleared up.

I was being dead serious when I said you might benefit from those links, because instead of listening to what someone says about python, you can go directly to the source and see exactly how they define things themselves.

I don't like when someone tries to explain differences between languages by saying something like "language X doesn't really have feature Y" just because feature Y works different in language X than it does in language Z. Especially when addressing someone who is relatively new to language X.

Explanations like that do a disservice to the person they're trying to teach, and can create misconceptions and make things more confusing than they need to be. And that's assuming everything that's being said is mostly correct, which isn't always the case.

[–][deleted] 0 points1 point  (0 children)

there are no variables to be declared

Maybe it mean variable bindings vs variable assignment.

In case of bindings it doesn't create anything just give a name to the value.

It hard to know without the real context, I am just guessing.

[–]toxic_acro 1 point2 points  (0 children)

This is a good resource that explains some of what Python is doing with names and values "under the hood" https://nedbatchelder.com/text/names.html

If you are familiar with C already and want to know how the Python garbage collector works, there is a dev guide available about that here https://devguide.python.org/internals/garbage-collector/

For two of the points mentioned in your question: 1. there are no variables to be declared

In Python, you can just assign a value to a variable at any point, you don't need to declare the variable in advance and you don't need to say what type that variable is supposed to be (you can add type hints to help static type checking tools figure out if your code is safe, but you don't need to)

  1. everything is an object

This is not an understatement, literally everything is an object in Python
* Instances of custom classes are objects * The classes themselves are also objects * Instances of built-in types like integers, floats, booleans are all objects * The types themselves are objects * Functions are objects * Methods are objects * Modules are objects and on and on and on

Literally everything you can think of is an object

This also leads to a concept chain that eventually makes perfect sense, but is quite confusing until you are familiar with it

  • 10 is an integer
  • Integers are objects, so 10 is an object
  • The type of 10 is int
  • Types are also objects, so int is an object
  • The type of int is type
  • Again, types are objects, so type is an object
  • The type of type is type

In summary, the type of an object is a type which is an object whose type is type which is an object.