I tried to solve the following problem the last few hours but it didn't work out.
I have two python files, one for creating classes and the second one for actually working with those classes.
I create the class hh
(=households) as follows but we can focus only on the fltCash
attribute because that's where the issue occurs; I used setters/getters only for this attribute at the bottom of the code as I was hoping that it could fix this issue.
# agent: households
class hh(agent):
def __init__(self, intId = 0,
# Income
fltCash = [100],
fltWage = [25],
fltInterest = [25],
fltDividend = [25],
fltTransfer = [25],
# Employment
intEmployerId = [-1],
# MinWage Preferences
fltIncomeweight = 1,
fltLoyalty2Employer = [-1],
# Utility Function
fltPref_a = 0.5,
fltPref_b = 1,
# Variables
intMinWage = -1
):
# def in superclass agent
agent.__init__(self, intId)
# def in childclass hh
self.fltCash = fltCash
self.fltWage = fltWage
self.fltInterest = fltInterest
self.fltDividend = fltDividend
self.fltTransfer = fltTransfer
self.intEmployerId = intEmployerId
self.fltIncomeweight = fltIncomeweight
self.fltLoyalty2Employer = fltLoyalty2Employer
self.fltPref_a = fltPref_a
self.fltPref_b = fltPref_b
self.intMinWage = intMinWage
## Setter / Getter Section
@property
def fltCash(self):
return self.__fltCash
@fltCash.setter
def fltCash(self, value):
if value.isdigit():
self.__fltCash = value
else:
print("Ungueltiger Wert fuer fltCash: {}".format(value))
In another file I define my main class where I create an object from this class. However, right after initializing the fltCash
attribute of this object contains not a list with only the default value but a list which various integers which seem to be random to me.
Has anyone a clue what's the reason for this behaviour and/or how to avoid it?
Any help would be highly appreciated.
Best Lukas
EDIT: Here is a simplified version of my code, I figure it's very stupid bug in my code but I can't find it:
import random as rd
class agent:
def __init__(self, intId = 0):
self.intId = intId
class hh(agent):
def __init__(self, intId = 0, fltCash = [100]):
# def in superclass agent
agent.__init__(self, intId)
# def in childclass hh
self.fltCash = fltCash
class firm(agent):
def __init__(self, intId = 0, fltCurrentAssets = [50]):
# def in superclass agent
agent.__init__(self, intId)
# def in childclass hh
self.fltCurrentAssets = fltCurrentAssets
def main():
### create agents
luke = hh(intId=3)
univie = firm(intId=2)
## HERE I'D EXPECT [100] AND [100]
print(luke.fltCash)
print(univie.fltCurrentAssets)
### action
i = 0
while True:
fltTransfer = rd.randrange(0,51)
if luke.fltCash[i] > univie.fltCurrentAssets[i]:
luke.fltCash.append(luke.fltCash[i] - fltTransfer)
univie.fltCurrentAssets.append(fltTransfer + univie.fltCurrentAssets[i])
else:
luke.fltCash.append(fltTransfer + luke.fltCash[i])
univie.fltCurrentAssets.append(univie.fltCurrentAssets[i] - fltTransfer)
i += 1
if luke.fltCash[i] < 0 or univie.fltCurrentAssets[i] < 0:
print("Game over! Luke: {}, \n Univie: {}".format(luke.fltCash,\
univie.fltCurrentAssets))
del luke
del univie
break
if i > 10:
del luke
del univie
break