BetterGetterSetterMaker Utility

A little utility app that creates template getters & setters for classes.

I just had to create a class with loads of getters & setters, and typing @property & @xxx.setter and def xxx…blah blah over and over and over again drove me to this :slight_smile:

So, for example,
enter “CRM” for class name.
enter “name”, “address”, “contact” in the properties (without the quotes) , each on a new line.
Click “Go” and you’ll get this :

class CRM:
  def __init__(self, **params):
    self.name = params['name'] if 'name' in params else None
    self.address = params['address'] if 'address' in params else None
    self.contact = params['contact'] if 'contact' in params else None

  @property
  def name(self):
    return self.name
  @name.setter
  def name(self,value):
    self.name = value

  @property
  def address(self):
    return self.address
  @address.setter
  def address(self,value):
    self.address = value

  @property
  def contact(self):
    return self.contact
  @contact.setter
  def contact(self,value):
    self.contact = value

Just cut/paste the resulting code.

Hopefully it will be useful to someone. Source available if anyone gives a stuff …

(link updated)

3 Likes

Wow, I have to bookmark your app!!! Great!

1 Like

Hi David,

I think this won’t quite work. This is because, when you have a custom property, accessing it results in a function call. So something like:

    @property
    def address(self):
      return self.address

…is actually an infinite recursion, because it keeps calling itself! (Sure enough, I tried to use your CRM class and got a “maximum call stack size exceeded” error.)

The usual way of handling this is to have private attributes (prefixed with _ by convention), which you use to store the actual values of your properties. Eg:

@property
  def name(self):
    return self._name
  @name.setter
  def name(self,value):
    self._name = value

Good point - I had a different version running here, fell foul of that myself a while back

That was careless - I will put a new version up shortly.

It has been updated, sorry about that anyone who used it!

2 Likes

It was a great initiative and if everything has to be triple checked before posted nothing will ever be shared and enjoyed by others…

1 Like

truly awesome work, thank you so much!!!
would love to see this as a default implementation by the anvil team.

1 Like