0

I am working with 2 classes defined as zone and system. The system is supposed to contain several zones, but the number can vary. So, I have defined for example the class zone as such:

class zone(object):
    def __init__ (self,
        nameZone = None,
        height = None,
        width = None):

        self.nameZone = nameZone
        self.height = height
        self.width = width   

and the class system as follows:

   class system(object):
    def __init__(self,
                 SystemName = None,
                 NumberOfZones = None,
                 zone = None):

        self.SystemName = SystemName
        self.NumberOfZones = NumberOfZones
        self.zone = zone

So for example I would like to know how do I go about defining the class SYSTEM with a variable number of parameters? Like for example I could have:

 building = system(2, apart1, apart2)
 building2 = system(3, apart1,apart2,apart3)

where of course apart1, 2, and 3 are defined as class zone.

I have tried something like:

for i in range(self.NumberOfZones)
         self.zone = zone

But this doesn't work obviously.

tshepang
  • 12,111
  • 21
  • 91
  • 136
caran
  • 59
  • 6

3 Answers3

0

You can use the variable args features.

class system(object):
    def __init__(self, SystemName, *zones):
        self.SystemName = SystemName
        self.zones = list(zones)

    # The number of zones is the length of the zones list.

Then you can just put any number, without even having to specify how many. Python will just collect all paramenters after the name as zones.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • 1
    You don't need to call `list(zones)` as zones is a list. – Jakob Bowyer Aug 12 '11 at 08:30
  • The problem I have now is this: AttributeError: 'zone' object has no attribute 'append' . Because I am using something like this: self.fileinwhichiwrite.append(line), so that I can write all the results I want to print out in a text file. Is there a way arround using this self.file.append(line) ? – caran Aug 12 '11 at 08:40
  • @Jakob, no, zones is a tuple. If you want it mutable after (such as append to it) you need to convert it to a list. – Keith Aug 12 '11 at 09:12
  • @caran I'm not sure what you're talking about. There is no `self.file` in your example. – Keith Aug 12 '11 at 09:13
  • Yes, I omitted that in my example cos I thought it was not important.In fact my full class definition would have also a line for this like: def __init__(self, SystemName = None, NumberOfZones = None, zone = None, filelines = None): self.SystemName = SystemName self.NumberOfZones = NumberOfZones self.zone = zone if filelines is None: self.filelines = [] else: self.filelines = filelines – caran Aug 12 '11 at 09:21
0
class System(object):
    def __init__(self, system_name = None, *zones):
    self.system_name = system_name
    self.number_of_zones = len(zones)
    self.zones = zones

I cleaned up to PEP8 slightly and left in number_of_zones.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

Using the varargs is OK, but I would say it is clearer to pass explicitly a list of zones. That way you can add different named parameters in the future if needed.

fortran
  • 74,053
  • 25
  • 135
  • 175