By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,526 Members | 1,618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,526 IT Pros & Developers. It's quick & easy.

Re: why am I not allowed to redefine a class ?

P: n/a
Stef Mientki wrote:
hello,

although this is not a real problem for me,
it was caused by a copying, instead of moving, a piece of code.
But I don't understand at all why the code below gives the error.
class derived_class, is defined twice,
the error is cuase by the second instance creation "test2="
for me even weirder, if I create test2 in another module, everything
works perfect ???
Any explanation would be welcome.
A different version of what Arnaud D. said:
==== start of code ===
class base_class ( object ) :
def __init__ ( self ) :
pass

class derived_class ( base_class ) :
def __init__ ( self ) :
base_class.__init__ ( self )

class final_class_1 ( derived_class ) :
In this line, 'derived_class' is evaluated when the class is defined
def __init__ ( self ) :
derived_class.__init__ ( self )
In this line, 'devired_class' is evaluated when the class is
instantiated and __init__ is called.
test1 = final_class_1 ()
Here, the object corresponding to 'derived_class in f_c_1.__init__is the
same as when f_c_1 was created.
class derived_class ( base_class ) :
def __init__ ( self ) :
base_class.__init__ ( self )

test2 = final_class_1 ()
Here, object corresponding to 'derived_class in f_c_1.__init__is no
longer the same as when f_c_1 was created.

==== end of code =====

==== error meassage =====
Traceback (most recent call last):
File "D:\Data_Python_25\PyLab_Works\module1.py", line 19, in <module>
test2 = final_class_1 ()
File "D:\Data_Python_25\PyLab_Works\module1.py", line 11, in __init__
derived_class.__init__ ( self )
TypeError: unbound method __init__() must be called with derived_class
This refers to the second 'derived_class', and f_c_1 ia *not* a subclass
of *that* class object.
instance as first argument (got final_class_1 instance instead)
tjr

Nov 9 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.