can't instantiate following inner class

P

Pyenos

class One:
Two() #can't instantiate
class Two:
Three() #can't instantiate
class Three:pass
 
L

Larry Bates

Pyenos said:
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
 
B

buffi

Pyenos said:
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)
 
G

Gabriel Genellina

At said:
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.
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
 
G

Gian Mario Tagliaretti

Larry said:
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
 
G

Gian Mario Tagliaretti

Gabriel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top