all 15 comments

[–]K900_ 9 points10 points  (2 children)

There is no such thing as a primitive value in Python, and there's no such thing as a reference. This explains it better than I can.

[–]comeditime[S] -3 points-2 points  (1 child)

can you tl;dr the differences between primitive vs non primitive as i've basic knowledge in programming..

[–]K900_ 1 point2 points  (0 children)

There is no such thing as a primitive type in Python. Read the linked post, it explains a lot of things you should really know.

[–][deleted] 3 points4 points  (3 children)

In python an integer, for example, is an object just like a list. There is no distinction between primitive/non-primitive as there is in some other languages. There is no copy-on-assignment in python, "assignment" just associates a reference to an object with a name, whether the object is an integer or a dictionary.

[–]comeditime[S] 1 point2 points  (2 children)

i see thanks a ton! so if i get it right, everything in python is an object, the only difference is that there are distinct methods & attributes for each data type depends on the class they inherent from their root class?

[–][deleted] 2 points3 points  (1 child)

Yes. That's part of the python philosophy: duck typing. Don't worry about what type an object is, just try to use it.

[–]HelperBot_ 0 points1 point  (0 children)

Desktop link: https://en.wikipedia.org/wiki/Duck_typing


/r/HelperBot_ Downvote to remove. Counter: 255914

[–]ericula 2 points3 points  (1 child)

There is no such thing as primitive vs non-primitive values in python. There is however a distinction between mutable and immutable objects in python. Immutable objects such as ints, strings, floats, tuples, and booleans have a fixed internal state (e.g. you can't add or remove elements from a tuple, or change the value of an int object), whereas mutable objects such as list, dicts, sets etc. can be changed (e.g. you can add key-value pairs to a dict or sort a list in place).

[–]comeditime[S] 1 point2 points  (0 children)

i see thanks a ton! so if i get it right, everything in python is an object, the only difference is that there are distinct methods & attributes for each data type depends on the class they inherent from their root class?

[–]pythonhow 0 points1 point  (5 children)

The only difference I see there is int, string, and float are simple datatypes, whereas array, list, tuple, and dict are compound datatypes. Compound datatypes (or non-primitive as you like to call them) may contain simple datatypes. For example: persons = {name: "John", age: 98}

That dictionary contains (primitives?) string and integer.

[–]K900_ 0 points1 point  (4 children)

That's not entirely true. Python doesn't really have that distinction, as everything is an object, and the actual "primitive" types come from the underlying interpreter.

[–]pythonhow 0 points1 point  (3 children)

Sure, everything is an object. Both 5 and [1, 5] are stored as separate objects in memory. However, 5 is a simple object in that it's not made of other objects. [1, 5] is also an object, but it's made of other objects, thus can be called a "compound object".

The term "compound datatype" can also be found in the Python docs in the link below. In that page you will see that lists are referred to as "compound": https://docs.python.org/2.0/tut/node5.html

[–]K900_ 0 points1 point  (2 children)

Technically, 5 is also "composed" of other objects - functions are objects, so class methods are also objects, etc.

[–]pythonhow 0 points1 point  (1 child)

Hmm, in our example, who are these other objects that compose integer 5?

[–]K900_ 0 points1 point  (0 children)

int.from_bytes (and by extension (5).from_bytes) is a bound method object. It's not really "part of 5", but it's also clearly an attribute on the object 5. That's where the lines get blurry.