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

can't instantiate following inner class

P: n/a
class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass

Dec 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Pyenos wrote:
class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass
You keep posting examples with the same problems
that others have addressed. It appears you are trying
to write Python in a way that some "other" language
works. You really should see the posted solutions and
go through the tutorial before posting again.

Note the following is "normally" not used Python:
Two()
This would instantiate a Two class that wouldn't be bound
to anything so you could never access it again in the
future. It would then be thrown away by garbage collection.
Proper way is:

class One:
def __init__(self):
self.Two=Two()

Of course Two must be a proper class definition also.

class Two:
def __init__(self):
self.Three=Three()

class Three:
pass

-Larry
Dec 28 '06 #2

P: n/a
Pyenos wrote:
class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass
Python parses code from top to bottom.
Since it first tries to read the class One and finds the class Two
inside it, it throws an error since it is not defined yet.

Reverse the order and it will work (like this:)

class Three:pass
class Two:
Three()
class One:
Two()

Of course this makes no sense whatsoever as the poster above me wrote.
The reason that you can use classes in the order the second poster
wrote is because of init not being called until you make an instance of
that class, and by then all classes are loaded.

/buffi (buffis.com)

Dec 28 '06 #3

P: n/a
Larry Bates wrote:
Proper way is:

class One:
def __init__(self):
self.Two=Two()

Of course Two must be a proper class definition also.

class Two:
def __init__(self):
self.Three=Three()

class Three:
pass
just as a side note probably it would be better to use new style classes in
newly written code:

class One(object):
def __init__(self):
whatever

don't forget to call __init__ on new style classes otherwise you can pass
arbitrary arguments when instantiating the class e.g.:

one = One(a, b)

but python will silently ignore them and you probably won't like it...

cheers
--
Gian Mario Tagliaretti
Dec 28 '06 #4

P: n/a
At Wednesday 27/12/2006 23:55, Gian Mario Tagliaretti wrote:
>class One(object):
def __init__(self):
whatever

don't forget to call __init__ on new style classes otherwise you can pass
arbitrary arguments when instantiating the class e.g.:

one = One(a, b)

but python will silently ignore them and you probably won't like it...
I don't understand what you say here.
>>class One(object): pass
....
>>a = One(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: default __new__ takes no parameters

How do you make Python silently ignore the arguments?
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 28 '06 #5

P: n/a
Gabriel Genellina wrote:
>>class One(object): pass
...
>>a = One(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: default __new__ takes no parameters

How do you make Python silently ignore the arguments?
sorry, my bad, that beauvoir was python 2.2 it was fixed in python 2.3 (I
didn't know since I always override __init__) and it will raise a TyoeError
as you have pointed out.

cheers
--
Gian Mario Tagliaretti
Dec 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.