Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 01:49 PM
imchi imchi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 171 imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level)imchi User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 37 m
Reputation Power: 26
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)

Reply With Quote
  #2  
Old December 10th, 2012, 02:56 PM
SuperOscar SuperOscar is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 48 m 37 sec
Reputation Power: 65
Quote:
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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Simple inheritance (newbie)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap