#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    178
    Rep Power
    28

    Simple inheritance (newbie)


    Hi,

    When I execute this code, I get "TypeError: must be type, not classobj".
    Why is this not working as expected?
    Thanks.

    Payment.py:
    Code:
    class Payment:
        def __init__(self):
            self.amount = 0
        def __init__(self, amount):
            self.amount = amount
        def get_amount(self):
            return self.amount
    
    class CCPayment(Payment):
        def __init__(self, card_no, amount):
            super(CCPayment, self).__init__(amount)
            self.card_no = card_no
        def get_card_no(self):
            return self.card_no
    Calling code:
    Code:
    from Payment import CCPayment
    from Payment import Payment
    
    a = CCPayment('1234', 10)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    Originally Posted by imchi
    When I execute this code, I get "TypeError: must be type, not classobj".
    Because in Python 2, not specifying the base class means you are using the old class system.

    To fix in Python 2, use:

    Code:
    class Payment(object):
    For the rest of your code, it (almost) works in Python 3 as is. However, you cannot use multiple definitions for __init__ (or any other function for that matter); instead, you should write the definition so that it fits all uses:

    Code:
    class Payment:
        def __init__(self, amount=0):
            self.amount = amount

    Comments on this post

    • imchi agrees : Thanks!
    • Dietrich agrees
    • Nyktos agrees
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)

IMN logo majestic logo threadwatch logo seochat tools logo