Skip to main content
edited tags
Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Improve code formatting, shorten sentences, modify title to make it easier to understand at first glance
Source Link

Python property with public getter withand private setter property

I have a python property like this declared inside Foo:

class Foo: @property def maxInputs(self): return self._persistentMaxInputs.value   @maxInputs.setter def maxInputs(self, value): self._persistentMaxInputs.value = value 

I use that property to always call "_persistentMaxInputs.value" object when calling "self.maxInputs" from within my class FooCurrently, the value of maxInputs can be get and set by everyone. Now i

However, I want to allow owners of Foos' instanceseveryone to get "maxInputs" but not to set "maxInputs". I know i could just remove the settervalue of the maxInputs, but in that case i also couldn't useit should only be set inside of the setter from within FooFoo class.

So is there a way to declare a private property setter with a private setter and a public getter?

Python public getter with private setter property

I have a python property like this declared inside Foo:

class Foo: @property def maxInputs(self): return self._persistentMaxInputs.value @maxInputs.setter def maxInputs(self,value): self._persistentMaxInputs.value = value 

I use that property to always call "_persistentMaxInputs.value" object when calling "self.maxInputs" from within my class Foo. Now i want to allow owners of Foos' instances to get "maxInputs" but not to set "maxInputs". I know i could just remove the setter, but in that case i also couldn't use the setter from within Foo.

So is there a way to declare a private property setter with a public getter?

Python property with public getter and private setter

I have a python property like this:

class Foo: @property def maxInputs(self): return self._persistentMaxInputs.value   @maxInputs.setter def maxInputs(self, value): self._persistentMaxInputs.value = value 

Currently, the value of maxInputs can be get and set by everyone.

However, I want to allow everyone to get the value of the maxInputs, but it should only be set inside of the Foo class.

So is there a way to declare a property with a private setter and a public getter?

Source Link
MorgoZ
  • 2.1k
  • 5
  • 29
  • 55

Python public getter with private setter property

I have a python property like this declared inside Foo:

class Foo: @property def maxInputs(self): return self._persistentMaxInputs.value @maxInputs.setter def maxInputs(self,value): self._persistentMaxInputs.value = value 

I use that property to always call "_persistentMaxInputs.value" object when calling "self.maxInputs" from within my class Foo. Now i want to allow owners of Foos' instances to get "maxInputs" but not to set "maxInputs". I know i could just remove the setter, but in that case i also couldn't use the setter from within Foo.

So is there a way to declare a private property setter with a public getter?