Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[[' ']*3]*3 buggy behavior?
#7
(Sep-07-2021, 04:26 PM)deanhystad Wrote:
Quote:I'm down to trying to understand when an assignment creates a new object vs references an existing object. Gotta do more reading on this subject. Perhaps immutable objects always get referenced and static things like a string get copied.
Immutable things, what you call static, are never copied. There is no reason to make copies of an immutable object because it's value cannot be changed. Not only are immutable objects never copied, Python tries to reuse immutable objects. This code reuses 'Hello World!' for x and y but I managed to trick it with z. Notice that the ID for x and y shows they reference the same str object, but z is a different str.
x = 'Hello World!'
y = 'Hello' + ' ' + 'World!'
z = ''.join(['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'])
print(x, y, z, id(x), id(y), id(z))
Output:
Hello World! Hello World! Hello World! 2103065666736 2103065666736 2103069140528
Python does an even better job reusing int objects.
x = 1
y = 1 * 1
z = int(input('Type 1 '))
print(x, y, z, id(x), id(y), id(z))
Output:
1 1 1 2816730622256 2816730622256 2816730622256
All three variables reference the same int object. Python can be less clever than when reusing strings. Python has a cache of ints. This program finds the range of cached ints for my Python (3.9.0) is -5 to 256.
cached = False
for x in range(-10000, 10000):
    y = int(x * 2 / 2)
    if (x is y) != cached:
        cached = not cached
        print(x, cached)
Output:
-5 True 257 False
"referenced" is not a concept in Python. Whenever your program creates an object, even numbers are objects in Python, the program returns a handle, or reference, to the object. This is your program's only view into the object. The handle is the address of the Python object in memory, but that is really an implementation detail and does not affect programs. When you add "1 + 2" you are actually calling a method of the int class and passing it two int objects. The method calls methods of the objects to get their value, adds the values together, and creates a new int object (or reuses a cached int object) to return the value. I guess you could say everything in Python is referenced and there is no such thing as "pass by value".

"when (does) an assignment creates a new object". Assignment never creates a new object. Assignment creates (or reassigns) a VARIABLE to reference the object. Object/instance creation occurs on the right hand side of the "=". Your question should be which operations create new objects and which don't.

Any operation involving immutable objects that produces a different value creates a new object. Adding two ints creates a new int. Concatenating two strs creates a new str. The answer is more complicated for mutable objects. Appending something to a list does not create a new list, but slicing a list does, even though the slice contains objects from the original list. Copy creates a new list, but unless you do a deep copy the new list contains the same objects as the source list. The only hard and fast rule for mutable objects is "read the docs" which should tell you if the operation is "in place" or if it returns a new object.

Thanks for the detailed reply deanhystad! Gotta shake those old concepts, object-oriented programming is still new to me.
(btw, 'static' was a typo, I meant 'mutable')
Reply


Messages In This Thread
[[' ']*3]*3 buggy behavior? - by shadowphile - Aug-05-2021, 10:16 PM
RE: [[' ']*3]*3 buggy behavior? - by deanhystad - Aug-06-2021, 03:22 AM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Aug-06-2021, 07:40 PM
RE: [[' ']*3]*3 buggy behavior? - by naughtyCat - Aug-18-2021, 03:35 PM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Aug-18-2021, 07:26 PM
RE: [[' ']*3]*3 buggy behavior? - by deanhystad - Sep-07-2021, 04:26 PM
RE: [[' ']*3]*3 buggy behavior? - by shadowphile - Sep-07-2021, 05:28 PM

Forum Jump:

User Panel Messages

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