mutable

Python updating list from constructor by object.attribute

Python updating list from constructor by object.attribute Question: If a class is created with the attributes: name and list and the name attribute has a default value and the list is appending the name. Is it possible in somehow when I create an object "a" and type "a.name = ‘x’ " that this ‘x’ will …

Total answers: 1

Python Pandas: "Series" objects are mutable, thus cannot be hashed when using .groupby

Python Pandas: "Series" objects are mutable, thus cannot be hashed when using .groupby Question: I want to take the 2nd derivative of column[‘Value’] and place it into another column. There is also another column called [‘Cycle’] that organizes the data into various cycles. So for each cycle, I want to take the 2nd derivative of …

Total answers: 1

Dataclass-style object with mutable and immutable properties?

Dataclass-style object with mutable and immutable properties? Question: I have been playing around with dataclasses dynamically loaded with property names from a file and I am unable to find a way to create both ‘frozen’ and ‘non-frozen’ properties. I believe dataclasses only allow you to set all properites to frozen or non-frozen. As of now, …

Total answers: 4

are python3 mutable attributes shared?

are python3 mutable attributes shared? Question: In the code I instantiate two different objects of the same class, how is it possible that object1 alters object2‘s attributes? How can I keep different “self.mutable” variables? Where’s my mistake? 🙂 thanks class Class: mutable = {} immutable = 0 def change(self): self.immutable = 1 self.mutable[“key”] = “value” …

Total answers: 1

Understanding how mutable variables behave across different scopes

Understanding how mutable variables behave across different scopes Question: Newbie to Python here. While practicing working with lists on Codingbat, I realized there are behaviors I don’t understand concerning lists and how mutable variables behave. Sample below is derived from a few Codingbat problems to frame my questions… In getManipulatedNewList(), newnums is destroyed because it …

Total answers: 1

Python mutable NamedTuple

Python mutable NamedTuple Question: I am looking for a struct like data structure I can create multiple instances from and have some type hinting without being immutable. So I have something like this: class ConnectionConfig(NamedTuple): name: str url: str port: int user: str = “” pwd: str = “” client: Any = None But I …

Total answers: 6

Why are integers immutable in Python?

Why are integers immutable in Python? Question: I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or is the answer "that’s just how it is"? Edit: …

Total answers: 2

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable?

Is it possible to make wrapper object for numbers, e.g. float, to make it mutable? Question: In Python 3 everything is supposed to be an object, even numbers, but they’re immutable. Is it possible to create wrapper object for numbers, e.g. float, such that it would behave exactly as ordinary numbers except it must be …

Total answers: 2

Existence of mutable named tuple in Python?

Existence of mutable named tuple in Python? Question: Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Camelot import namedgroup Point = namedgroup(‘Point’, [‘x’, ‘y’]) p = Point(0, 0) p.x = 10 >>> p …

Total answers: 14

Correct Style for Python functions that mutate the argument

Correct Style for Python functions that mutate the argument Question: I would like to write a Python function that mutates one of the arguments (which is a list, ie, mutable). Something like this: def change(array): array.append(4) change(array) I’m more familiar with passing by value than Python’s setup (whatever you decide to call it). So I …

Total answers: 4