Hi,
I've noticed that if I initialize list of integers in the next manner: my_list = [0] * 30
It works just fine, even if I'll try to assign one element:
id( my_list[4] )
10900116 id( my_list[6] )
10900116 my_list[4] = 6 id( my_list[4] )
10900044 id( my_list[6] )
10900116
The change in the poision occurs becouse int() is an immutable object.
if I will do the same with a user-defined object, This reference
manipulating will not happen. So, every entry in the array will refer
to the same instance.
Is there a way to bypass it (or perhaps to write a self-defined
immutable object)? 6 1651
> The change in the poision occurs becouse int() is an immutable object. if I will do the same with a user-defined object, This reference manipulating will not happen. So, every entry in the array will refer to the same instance.
Is there a way to bypass it (or perhaps to write a self-defined immutable object)?
This has nothing to do with int being mutable or not. It only has to do
with _list_ being mutable, and of course teh semantics of the
_ * _ : list x int -> list
operator, which simply shallow copies the list. So assigning to some
l[i]
will replace that entry - regardless of what is stored there. And that
is all you do. The mutablity of an object only comes into place if you
try and do
l[i].some_mutating_op()
That catches many people by surprise - but you don't do such a thing :)
And besides: making an object immutable would mean that the only way to
create an instance would be the constructor - rendering the purpose of
the whole exercise somewhat moot - as then you'd have to call the
constructor individually for each index you want to alter anyway. Which
you have to do in the case of mutable objects, too. So - no gain there.
Regards,
Diez
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Mapisto" <ma*****@gmail.com> wrote: I've noticed that if I initialize list of integers in the next manner:
my_list = [0] * 30 It works just fine, even if I'll try to assign one element: id( my_list[4] ) 10900116 id( my_list[6] ) 10900116 my_list[4] = 6 id( my_list[4] ) 10900044 id( my_list[6] ) 10900116
The change in the poision occurs becouse int() is an immutable object.
if I will do the same with a user-defined object, This reference manipulating will not happen. So, every entry in the array will refer to the same instance.
Not at all. If you do the same thing,
class C:
pass
c = C()
a = [c]*12
.... etc., you should observe the same pattern with respect to
object identities. Mutability doesn't really play any role here.
Is there a way to bypass it (or perhaps to write a self-defined immutable object)?
Bypass what? What do you need?
Donn Cave, do**@u.washington.edu
Mapisto wrote: Hi,
I've noticed that if I initialize list of integers in the next manner:
my_list = [0] * 30
It works just fine, even if I'll try to assign one element:
id( my_list[4] ) 10900116 id( my_list[6] ) 10900116 my_list[4] = 6 id( my_list[4] ) 10900044 id( my_list[6] ) 10900116
The change in the poision occurs becouse int() is an immutable object.
No, it happens because you assign my_list[4] to a different object.
Obviously, 0 and 6 can't be located in the same place in RAM.
The difference lies in doing something like "my_list[n] = X" rather
than changing the state of a shared existing object as in something
like "my_list[n].f(X)".
if I will do the same with a user-defined object, This reference manipulating will not happen.
Really? class C:
.... pass
.... my_list = [C()]*30 id(my_list[4])
1003056 id(my_list[6])
1003056 my_list[4] = C() # Another instance id(my_list[4])
986048 id(my_list[6])
1003056
Ok, I've understood my mistake.
Now, my list contains a shared entry of an empty object. When an entry
is needed to be changed, I check if the entry is the shared empty
object; in that case I create a new unique instance. If the entry is
already a unique instance, I use it, so the empty object isn't touched.
Thanks,
Guy.
Mapisto wrote: Ok, I've understood my mistake.
Now, my list contains a shared entry of an empty object. When an entry is needed to be changed, I check if the entry is the shared empty object; in that case I create a new unique instance. If the entry is already a unique instance, I use it, so the empty object isn't touched.
It's probably less confusing if you make a list
of None, instead of a list of references to an
instance object you don't use anyway.
Mapisto wrote: Ok, I've understood my mistake.
Now, my list contains a shared entry of an empty object. When an entry is needed to be changed, I check if the entry is the shared empty object; in that case I create a new unique instance. If the entry is already a unique instance, I use it, so the empty object isn't touched.
If you know that you'll use all list entries, you
might as well do it right from the start. Instead
of:
l = [C()] * n
you can do:
l = [C() for i in range(n)] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Zalek Bloom |
last post by:
I am learning about difference between String and StringBuffer
classes. In a book it sayes that a String class is immutable, that
means that one an instance of String class is created, the string...
|
by: Batista, Facundo |
last post by:
I'm working on Decimal, and one of the PEP requests is Decimal to be
immutable.
The closer I got to that is (in short):
class C(object):
__slots__ = ('__x',)
def __init__(self, value):
|
by: Batista, Facundo |
last post by:
Thank you for your assistance.
I'll go in Decimal with a solution with property() and *single* underscore:
class C(object):
__slots__ = ('_x',)
def __init__(self, value):
self._x =...
|
by: Chris |
last post by:
Hi
Consider the following tuples:
>>> t = (,)
>>> u = (1,2)
>>> v = ('1','2')
>>> t
(, )
>>> u
(1, 2)
|
by: Andrew Quine |
last post by:
Hi
Just read this article http://www.artima.com/intv/choices.html.
Towards the end of the dicussions, when asked "Did you consider
including support for the concept of immutable directly in C#...
|
by: Ben Finney |
last post by:
Howdy all,
I've recently packaged 'enum' in PyPI. In its description, I make the
claim that it creates "immutable" enumeration objects, and that the
enumeration values are "constant" values.
...
|
by: Ben Finney |
last post by:
Howdy all,
How can a (user-defined) class ensure that its instances are
immutable, like an int or a tuple, without inheriting from those
types?
What caveats should be observed in making...
|
by: ssecorp |
last post by:
>>h = "aja baja"
'aja bajae'
|
by: k3xji |
last post by:
Hi all,
This will probably be a long question/short answer, sorry, but I have
wandered net about the subject and really feel cannot find just enough
information.I want to ask my question by...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |