如何防止在 __init__() python 中创建对象

How to prevent object creation in __init__() python

正在练习 python OOP 和 TDD。以下代码创建了 class office of subclass room 的对象,即使给出的名称以数字开头也是如此。我该如何预防?感谢大家的帮助

class Room(object):

    """Create general features of a general room
    Each room has a certain capacity depending on its type(an office or a living room)
    """

    def __init__(self):
        super(Room, self).__init__()
        self.capacity = '';

class Office(Room):

    """
    Add specific features to make a room an office.
    An office has a name and a space for maximum of 4 people.
    """

    def __init__(self, rname):

        #create object if name does not start with a digit
        if not (rname[0].isdigit()):
            super(Office,self).__init__()
            self.name = rname #office name
            self.capacity = 4 #number of spaces per office

这是测试用例:

class OfficeTests(unittest.TestCase):

def setUp(self):
    self.office = Office('BLUE')
    self.office1 = Office('12345')

def test_office_is_of_class_office(self):
    self.assertTrue(isinstance(self.office, Office), 
       msg = "Should create      an object of class Office")

def test_office_is_of_class_room(self):
    self.assertTrue(isinstance(self.office, Room), 
        msg = "Should create an object of class Office of subclass Room")

def test_office_capacity_is_4(self):
    self.assertEqual(self.office.capacity, 4, 
          msg= "An office has a maximum of 4 ")

def test_office_has_a_name(self):
    self.assertEqual(self.office.name,'BLUE', msg = "Should assign name to an office created.")

def test_office_name_does_not_start_with_a_digit(self):
    print(self.office1, self.office)
    self.assertTrue(self.office1 == None, msg = "Office name can only start with a letter.")

def tearDown(self):
    self.office = None

以及测试用例的结果

....(<room.Office object at 0x7fa84dc96a10>, <room.Office object at 0x7fa84dc968d0>)

F...

看了很多,有了答案。 在对象创建过程中,newinit之前被调用。因此通过重写new可以控制对象的创建。 就我而言,我只想创建名称以字母开头的房间(办公室)。这是所做的:

def __new__(cls,rname):

    '''We need this method since creation of an office object is depended on the first character of its name'''
    if not rname[0].isdigit():
        return Room.__new__(cls, rname)

    else:
        return None #don't create object here. __init__() is not called too.