Re: New to OOP - Class Override Problem

30 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Lateef Jackson

ungelesen,
26.06.2012, 10:07:0426.06.12
an cha...@googlegroups.com
Well I don't know anything about the book Python3 OOP. I am sending you the version (ex3.py) that is working for me in Python3. Note this will not work in Python2! Personally will continue to use the old syntax because I don't think the "super" really buys you anything ex:
class Family(Entity):
    def __init__(self, name, email, phone):
        Entity.__init__(self, name, email)
        self.phone = phone

This also depends on what you are trying to do but I have found this method works in both 2 & 3 and I know what it is doing. I am not sure I understand the use case for super.




On Mon, Jun 25, 2012 at 2:00 PM, D.C. Parris <parr...@gmail.com> wrote:
Hi all,

I just started reading Python3 OOP (Dusty Phillips), and I am using Python3, Idle3 interpreter on Kubuntu Linux 11.10 for playing with the examples.  In following the override example in Chapter 3, I have an entity class with a standard subclass and one that overrides the superclass.  Even following the example almost verbatim, my Family class fails when I try to create a Family entity at run-time.  The Supplier subclass works fine, but when I try to override the Entity class in the Family class, it fails.

The sample class in the book is "Contact" (I call it Entity).  The author is showing how, if you have a class with a name and e-mail, you can create a subclass of Contacts with phone numbers:

#Class
class Entity:
    all_entities = []
   
    def __init__(self, name, email):
        self.name = name
        self.email = email
        Entity.all_entities.append(self)

class Supplier(Entity):
    def order(self, order):
        print("If this were a real system we would send "
        "{} order to {}".format(order, self.name))

class Family(Entity):
    def __init__(self, name, email, phone):
        super().__init__(name, email)
        self.phone = phone


# Sample run
>>> f = Family("Don", "d...@somedomain.net", "7043657300")
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    f = Family("Don", "d...@somedomain.net", "7043657300")
TypeError: __init__() takes exactly 5 arguments (4 given)

I do note that the __init__() shows empty.  In another iteration, the () had values in them (the variable names), and I received a lower number of parameters than were really supposed to be.  Can anyone tell me what I am missing? 

Thanks!
Don

--
You received this message because you are subscribed to the Google Groups "Charlotte Python Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/charpy/-/0WVs42YdrKMJ.
To post to this group, send email to cha...@googlegroups.com.
To unsubscribe from this group, send email to charpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/charpy?hl=en.



--
Lateef Jackson
ex3.py
ex2.py

Don Parris

ungelesen,
26.06.2012, 23:53:3226.06.12
an cha...@googlegroups.com
Thanks Lateef.  I compared your ex3 file with my script and found the only difference was in a blank line.  I also discovered that all 3 files work just fine in a console window, and that your ex3 file runs fine in Idle3, but my script does not work in Idle3.  That strikes me as strange.  Anyway, I've got it working now.  Again, I did not see any difference really between your file and mine.

Thanks again for taking a look,
Don
D.C. Parris, FMP, Linux+, ESL Certificate
Minister, Security/FM Coordinator, Free Software Advocate
GPG Key ID: F5E179BE

Lateef Jackson

ungelesen,
03.07.2012, 15:03:0703.07.12
an cha...@googlegroups.com
Can you email me the file you are having trouble with I want to make sure it is not a syntax error.
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten