Python Forum
Question about primitive variables.
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about primitive variables.
#1
Hi all,

I have started to learn what primitive variables are vs Referenced ones.

The problem is, that once I have read that python is fully referenced, object orientated language, which have no primitive data types, I now read the following online:

"In Python there are four primitive variable types, which are listed below.
Integers
Floats
Strings
Characters"

So, can anyone please tell my what the correct answer is?
Does python has primitive variable types?
or even Strings and integers are referenced variables?

Thanks in advance,
Nirel.
Reply
#2
What's the source? What different, other than semantics, are they claiming? The difference can be important in other languages.

I wouldn't say Python has primitives. Even ints are referenced, and there's even an optimization for small ints where this can be observed in CPython (the standard Python implementation)
Output:
>>> 2**2 is 2**2 True >>> 2**32 is 2**32 False
Reply
#3
In Python the distinction is generally made between mutable and immutable objects. Mutable objects can be changed in place, and are stored by reference. Immutable objects must be replaced, and are stored by value. (There are exceptions to this, but they're not that big a deal). So, the immutable types (what you would call primitive) include ints, floats, complex, strs, tuples, bytes, and frozensets.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Apr-01-2017, 05:58 PM)micseydel Wrote: What's the source? What different, other than semantics, are they claiming? The difference can be important in other languages.

I wouldn't say Python has primitives. Even ints are referenced, and there's even an optimization for small ints where this can be observed in CPython (the standard Python implementation)
Output:
>>> 2**2 is 2**2 True >>> 2**32 is 2**32 False
OK, firstly I must say your example gave me a new question lol.
why "2**32 is 2**32" gives us output of "False"?
I mean, it is not only the same value but it is also the same type, shouldn't it be True? like the first example?

regarding the primitives I can give you for example someone which answer a question online:
http://stackoverflow.com/questions/63916...-primitive
stating "there are no primitive types in Python"

on the other hand there is this link:
https://en.wikiversity.org/wiki/Python_C...data_types
where they stated:
"In Python there are four primitive variable types, which are listed below.
Integers
Floats
Strings
Characters"

which made me not understand why some say this way and some the other way,
but by your answer and ichabod801 answer I guess they did a mistake on this site?


(Apr-01-2017, 05:59 PM)ichabod801 Wrote: In Python the distinction is generally made between mutable and immutable objects. Mutable objects can be changed in place, and are stored by reference. Immutable objects must be replaced, and are stored by value. (There are exceptions to this, but they're not that big a deal). So, the immutable types (what you would call primitive) include ints, floats, complex, strs, tuples, bytes, and frozensets.

OK, so I think I understand what you are saying.
You are basically saying that everything in python is an object, therefore we distinct between mutable and immutable objects.
Reply
#5
I think you can add the booleans to the list too. I don't call this things primitives or something else. I just checked out in translate.google.com what primitives mean. I don't care about the term. I just use them.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Apr-01-2017, 09:04 PM)wavic Wrote: I think you can add the booleans to the list too.

I thought about that, because they are a subclass of int. However, they are singletons, which seems to put them in another category.

@Nirelg, the term 'singleton' relates to your question about the is operator. The is operator check the id() of a value, which is where it is stored in memory. So two ints with the same value may not 'is' the same because they are stored in different locations. Singleton means there is only ever one instance of True or False. They are always stored in the same location. None is another singleton.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
This illustrate it.

In [1]: a = 42

In [2]: b = 42

In [3]: id(a)
Out[3]: 10915680

In [4]: id(b)
Out[4]: 10915680
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Apr-01-2017, 09:24 PM)ichabod801 Wrote:
(Apr-01-2017, 09:04 PM)wavic Wrote: I think you can add the booleans to the list too.

I thought about that, because they are a subclass of int. However, they are singletons, which seems to put them in another category.

@Nirelg, the term 'singleton' relates to your question about the is operator. The is operator check the id() of a value, which is where it is stored in memory. So two ints with the same value may not 'is' the same because they are stored in different locations. Singleton means there is only ever one instance of True or False. They are always stored in the same location. None is another singleton.
Ohh, now I got it.
But why would they have a different id?
I used the following code:
print type(2**11)
print type(2**2)
print id(2**11)
print id(2**11)
print id(2**2)
print id(2**2)
and the output is:
Output:
<type 'int'> <type 'int'> 46064336 46064324 45314908 45314908
They are both int after all, so why when I write twice 2**2 it gives me the same id yet when I do the same for 2**11 it gives them different ids?
Reply
#9
(Apr-01-2017, 10:27 PM)Nirelg Wrote: They are both int after all, so why when I write twice 2**2 it gives me the same id yet when I do the same for 2**11 it gives them different ids?
Integers between -5 and 256 are cached in Python.
This optimization strategy makes sense because small integers pop up all over the place,
and given that each integer takes 24 bytes, it saves a lot of memory for a typical program.

So id() show memory address.
>>> help(id)
Help on built-in function id in module builtins:

id(...)
    id(object) -> integer
    
    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)

>>> id(10)
491765920
>>> id(10)
491765920
>>> id(10)
491765920
>>> id(100)
491767360
>>> id(100)
491767360
>>> id(100)
491767360
>>> # Now over 256
>>> id(1000)
58284576
>>> id(1000)
51896640
Reply
#10
The canonical video(*) about this: https://www.youtube.com/watch?v=_AEJHKGk9ns

(*) On the whole, I deeply despise YouTube tutorial videos. But this one is too good to pass.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 167 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Question regarding local and global variables donmerch 12 5,074 Apr-12-2020, 03:58 PM
Last Post: TomToad
  Question about naming variables in class methods sShadowSerpent 1 2,002 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Basic Pyhton for Rhino 6 question about variables SaeedSH 1 2,141 Jan-28-2020, 04:33 AM
Last Post: Larz60+
  A question about global variables Goldberg291 3 4,003 Feb-02-2017, 10:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020