Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent object creation in __init__() python [duplicate]

Tags:

python

oop

init

Am practicing python OOP with TDD. The following code is creating an object of class office of subclass room even when given a name starting with a digit. How do I prevent it? Thanks guys for help

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

Here is the testcase:

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

And the results from the testcases

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

F...

like image 542
Meshack Mbuvi Avatar asked Dec 02 '25 20:12

Meshack Mbuvi


1 Answers

After reading much, have got the answer. During object creation, new is called before init.Thus by overriding new one can control object creation. In my case, I want to create only rooms(offices) whose name start with a letter. This is what have done:

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.
like image 72
Meshack Mbuvi Avatar answered Dec 05 '25 09:12

Meshack Mbuvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!