Answer the question
In order to leave comments, you need to log in
Error in factory. How to fix?
Please help me fix the error. I'm trying to put into practice the "Factory" design pattern.
from datetime import datetime, date, time
class GameElement(object):
def __init__(self, name):
self.name = name
class Boat(GameElement):
def __init__(self, name, peoples, speed, color, oars):
GameElement.__init__(self, name)
self.peoples = peoples
self.speed = speed
self.color = color
self.oars = oars
Boat.quantity += 1
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
class Raft(GameElement):
def __init__(self, name, peoples, speed, color, sails):
GameElement.__init__(self, name)
self.peoples = peoples
self.speed = speed
self.color = color
self.sails = sails
Raft.quantity += 1
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
class BoatsGenerator(object):
def factory(self, name, peoples, speed, color, oars):
print(str(name) + '::' + 'created boat')
#return Boat(name, peoples, speed, color, oars)
class RaftsGenerator(object):
def factory(name, peoples, speed, color, sails):
print(str(name) + '::' + 'created raft')
#return Raft(name, peoples, speed, color, sails)
class World(object):
def __new__(cls,*dt,**mp):
if cls.obj is None:
cls.obj = object.__new__(cls,*dt,**mp)
World.quantity += 1
boat1 = BoatsGenerator.factory(name='beda', peoples=8, speed=110, color='red', oars=2)
raft1 = RaftsGenerator.factory(name='abibas', peoples=3, speed=5, color='maroon', sails=0)
print(boat1)
print(raft1)
return cls.obj
obj = None
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
world1 = World()
world1.creared_at = datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
print(world1)
print('worlds quantity: ' + str(World.quantity))
[email protected] ~/python/boats3 $ python index.py
Traceback (most recent call last):
File "index.py", line 74, in <module>
world1 = World()
File "index.py", line 58, in __new__
boat1 = BoatsGenerator.factory(name='beda', peoples=8, speed=110, color='red', oars=2)
TypeError: unbound method factory() must be called with BoatsGenerator instance as first argument (got nothing instead)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question