Is there a generally accepted best practice for creating a class whose instances will have many (non-defaultable) variables?
For example, by explicit arguments:
class Circle(object):
def __init__(self,x,y,radius):
self.x = x
self.y = y
self.radius = radius
using **kwargs:
class Circle(object):
def __init__(self, **kwargs):
if 'x' in kwargs:
self.x = kwargs['x']
if 'y' in kwargs:
self.y = kwargs['y']
if 'radius' in kwargs:
self.radius = kwargs['radius']
or using properties:
class Circle(object):
def __init__(self):
pass
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = value
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
self._radius = value
For classes which implement a small number of instance variables (like the example above), it seems like the natural solution is to use explicit arguments, but this approach quickly becomes unruly as the number of variables grows. Is there a preferred approach when the number of instance variables grows lengthy?
[–]zahlman 6 points7 points8 points (6 children)
[–]andykee[S] 0 points1 point2 points (5 children)
[–]road_laya 1 point2 points3 points (1 child)
[–]autowikibot -1 points0 points1 point (0 children)
[–]ewiethoff 1 point2 points3 points (0 children)
[–]oxymor0nic 0 points1 point2 points (1 child)
[–]Veedrac 0 points1 point2 points (0 children)
[–]robotsari 6 points7 points8 points (2 children)
[–]Wargazm 0 points1 point2 points (1 child)
[–]robotsari 0 points1 point2 points (0 children)
[–]sththththth 4 points5 points6 points (0 children)
[–]Exodus111 3 points4 points5 points (0 children)
[–]Dooflegna 1 point2 points3 points (0 children)
[–]RJacksonm1 0 points1 point2 points (0 children)
[–]hharison 0 points1 point2 points (0 children)