Z
Z
zlodiak2015-12-02 20:21:29
Python
zlodiak, 2015-12-02 20:21:29

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))

This code has classes for Boat and Raft objects, as well as factories for each of them (BoatsGenerator, RaftsGenerator). The creation procedure itself takes place in the World class initializer. The result should be 2 objects, but my console displays an error message:
[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

1 answer(s)
A
AtomKrieg, 2015-12-02
@zlodiak

class BoatsGenerator(object):
  def factory(self, name, peoples, speed, color, oars):

remove self

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question