python - Best way to publicize instance attributes -


i'm new python, coming c#. know how publicize class attributes , methods. i'd publicize instance variables. having intellisense detect them ideal. also, please let me know if not pythonic or if should doing else.

class myclass(object):     class_attribute = "foo"      #this way i'm publicizing instance attributes.       #basically i'm using properties instead of instance attributes.     @property     def instance_property(self):         return self._instance_property      @instance_property.setter     def instance_property_set(self, value):         self._instance_property = value 

class myclass(object):     class_attribute = "foo"      def __init__(self,*args,**kwargs):         self.instance_property = "whatever" 

often it(setting required values) done in init function ... or through documentation, or setting invalid default value later check , inform consumer must set variable x before calling calculate() or whatever ... getters , setters not meant inform consumer variables should set

there no reason use getters/setters unless need work (not pass on variable)

a use case setter be

class gpio(object):     @property     def value(self):         return open("/sys/class/gpio/gpio141/value").read()      @value.setter     def set_value(self,val):          open("/sys/class/gpio/gpio141/value","w").write(val) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -