readonly

How to make dictionary read-only?

How to make dictionary read-only? Question: I have a class like: class A: def __init__(self): self.data = {} and at some moment I want to prohibit self.data fields modification. I’ve read in PEP-416 rejection notice that there are a lot of ways to do it. So I’d like to find what they are. I tried …

Total answers: 5

When should an attribute be private and made a read-only property?

When should an attribute be private and made a read-only property? Question: I don’t know when an attribute should be private and whether I should use property. I read recently that setters and getters are not pythonic but using the property decorator is OK. But what if I have attribute, that mustn’t be set from …

Total answers: 10

Immutable numpy array?

Immutable numpy array? Question: Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what’s the minimum set of methods that one has to override to achieve immutability? Asked By: NPE || Source Answers: You can make a numpy array unwriteable: a …

Total answers: 3

Read/Write Python Closures

Read/Write Python Closures Question: Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another …

Total answers: 8