immutability

Explaining appearent idiosyncrasy in Python when using tuples as elements in a set

Explaining appearent idiosyncrasy in Python when using tuples as elements in a set Question: If we execute the two following lines in Python: a = (1,) {a} it won’t cause any problems. Instead, if we execute the two following lines: a = ([1],) {a} now what we get is "TypeError: unhashable type: ‘list’". In both …

Total answers: 1

Default arguments in __init__ of class inheriting from int

Default arguments in __init__ of class inheriting from int Question: I feel like I’m losing my mind. I was working on a simple project involving a subclass of int, which may not be the best idea, but I need to inherit many of int‘s magic methods for integer operations. I added a default argument wraps=True …

Total answers: 1

python mypy error with immutable list as tuple

python mypy error with immutable list as tuple Question: I encounter a weird error while defining an immutable list type: import sys from re import split from typing import Tuple, List, NewType tokens = split("(d+.d+)", sys.argv[1]) # this works perfectly # Tokens = NewType("Tokens", List[str]) # print(Tokens(tokens)) # this doesn’t work … Tokens = NewType("Tokens", …

Total answers: 1

Can apply function change the original input pandas df?

Can apply function change the original input pandas df? Question: I always assume that the apply function won’t change the original pandas dataframe and need the assignment to return the changes, however, could anyone help to explain why this happen? def f(row): row[‘a’] = 10 row[‘b’] = 20 df_x = pd.DataFrame({‘a’:[10,11,12], ‘b’:[3,4,5], ‘c’:[1,1,1]}) #, ‘d’:[[1,2],[1,2],[1,2]] …

Total answers: 1

How to replace a character with another character in a list of string

How to replace a character with another character in a list of string Question: I have the following list of strings: a = [‘1234!zf’, ‘5678!ras’, ‘abcd!ggt’, ‘defg!z’, ‘hijk!’, ‘lmnk!reom’] I want to replace the 4th character with another character (the character will always be the same in every string, I just don’t know it, in …

Total answers: 2

Create a field which is immutable after being set

Create a field which is immutable after being set Question: Is it possible to create a Pydantic field that does not have a default value and this value must be set on object instance creation and is immutable from then on? e.g. from pydantic import BaseModel class User(BaseModel): user_id: int name: str user = User(user_id=1, …

Total answers: 3

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

What is size-mutable as per Python's pandas DataFrame class?

What is size-mutable as per Python's pandas DataFrame class? Question: What does size-mutable mean, in this context? “Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure” …

Total answers: 2

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