Reputation: 311
Please help solve the problem. I try use factory pattern for generate objects 'Boat':
from datetime import datetime, date, time
import pprint
import random
class Boat(object):
def __init__(self, name, max_peoples, max_speed, color_num, oars):
self.max_peoples = max_peoples
self.max_speed = max_speed
self.color_num = color_num
self.oars = oars
self.name = name
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
class BoatFactory(object):
def __init__(self, num):
return Boat(
name='beda_'+str(num),
max_peoples=random.randint(5, 10),
max_speed=(random.randint(5, 10)) * 10,
color_num=random.randint(1, 5),
oars=random.randint(1, 2)
)
class FloatFactory(object):
def __init__(self, num, factory):
return factory(num)
class World(object):
def __new__(cls,*dt,**mp):
if cls.obj is None:
cls.obj = object.__new__(cls,*dt,**mp)
return cls.obj
def __init__(self):
self.boats = []
for i in range(4):
self.boats.append(FloatFactory(num=i, factory=BoatFactory))
obj = None
if __name__ == '__main__':
world1 = World()
for boat in world1.boats:
print(boat)
But, after run script I get the following error message:
(python3_env)kalinin@kalinin ~/python3/python3_files/boats6 $ python
index.py Traceback (most recent call last):
File "index.py", line 61, in <module>
world1 = World()
File "index.py", line 55, in __init__
self.boats.append(FloatFactory(num=i, factory=BoatFactory))
File "index.py", line 41, in __init__
return factory(num)
TypeError: __init__() should return None, not 'Boat'
Please, help me to fix this code.
Upvotes: 0
Views: 94
Reputation: 2768
__init__
cannot return non-None
value, you can make factories being functions:
##class BoatFactory(object):
## def __init__(self, num):
## return Boat(
## name='beda_'+str(num),
## max_peoples=random.randint(5, 10),
## max_speed=(random.randint(5, 10)) * 10,
## color_num=random.randint(1, 5),
## oars=random.randint(1, 2)
## )
def BoatFactory(num):
return Boat(
name='beda_'+str(num),
max_peoples=random.randint(5, 10),
max_speed=(random.randint(5, 10)) * 10,
color_num=random.randint(1, 5),
oars=random.randint(1, 2)
)
##class FloatFactory(object):
## def __init__(self, num, factory):
## return factory(num)
def FloatFactory(num, factory):
return factory(num)
you can also make your class callable, see __call__
:
class BoatFactory(object):
def __call__(self, num):
return Boat(
name='beda_'+str(num),
max_peoples=random.randint(5, 10),
max_speed=(random.randint(5, 10)) * 10,
color_num=random.randint(1, 5),
oars=random.randint(1, 2)
)
class FloatFactory(object):
def __call__(self, num, factory):
return factory()(num)
class World(object):
def __new__(cls,*dt,**mp):
if cls.obj is None:
cls.obj = object.__new__(cls,*dt,**mp)
return cls.obj
def __init__(self):
self.boats = []
for i in range(4):
self.boats.append(FloatFactory()(num=i, factory=BoatFactory))
obj = None
or just change __init__
to __new__
:
class BoatFactory(object):
def __new__(self, num):
return Boat(
name='beda_'+str(num),
max_peoples=random.randint(5, 10),
max_speed=(random.randint(5, 10)) * 10,
color_num=random.randint(1, 5),
oars=random.randint(1, 2)
)
class FloatFactory(object):
def __new__(self, num, factory):
return factory(num)
but I find the first option most readable
Upvotes: 2