python - Properties and inheriting from instances -


so don't use oop , apparently don't understand thought did.

suppose have class (geographic) state:

class state(object):     @property     def population(self):         return self._population     @population.setter     def population(self,value):         if value < 0:             raise valueerror("population must not negative")         else:             self._population = value 

and virtually identical (just @ moment) class town:

class town(state):     @property     def population(self):         return self._population     @population.setter     def population(self,value):         if value < 0:             raise valueerror("population must not negative")         else:             self._population = value 

now suppose instantiate state , give specific population. how create instance of town inherits state instance's population? (temporarily, suppose - it's example.) or should using composition rather inheritance?

the way i'm thinking of it, should work:

s = state()  s.population = 10  t = town(s) --------------------------------------------------------------------------- typeerror                                 traceback (most recent call last) <ipython-input-141-00f052d998f0> in <module>() ----> 1 t = town(s)  typeerror: object.__new__() takes no parameters 

you not town is a state(unless live in singapore or hong kong!). state has a town. indicates composition.

note state doesn't have population attribute default. inheriting state not give town attributes:

class state(object):     @property     def population(self):         return self._population     @population.setter     def population(self,value):         if value < 0:             raise valueerror("population must not negative")         else:             self._population = value  s = state() print s.population  --output:-- raceback (most recent call last):   file "1.py", line 13, in <module>     print s.population   file "1.py", line 4, in population     return self._population attributeerror: 'state' object has no attribute '_population' 

so when say:

now suppose instantiate state , give specific population. how create instance of town inherits state instance's population?

...that doesn't make sense because town class has no knowledge of instances of state. obvious answer giving town instance same population state instance this:

class state(object):     @property     def population(self):         return self._population     @population.setter     def population(self,value):         if value < 0:             raise valueerror("population must not negative")         else:             self._population = value  class town(object):     def __init__(self, population):         self._population = population      @property     def population(self):         return self._population  s = state() s.population = 30 print s.population  t = town(s.population) print t.population 

using composition, this:

class state(object):     def __init__(self, name, *towns):         self.name = name         self.towns = towns       @property     def population(self):         total = 0         town in self.towns:             total += town.population         return total  class town(object):     def __init__(self, name, population):         self._population = population      @property     def population(self):         return self._population      @population.setter     def population(self,value):         if value < 0:             raise valueerror("population must not negative")         else:             self._population = value  detroit = town("detroit", 40) lansing = town("lansing", 100) detroit.population -= 10 print detroit.population print lansing.population  s = state("michigan", detroit, lansing) print s.population  --output:-- 30 100 130 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -