473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

can't instantiate following inner class

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

Dec 28 '06 #1
5 4518
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Haines Brown | last post by:
I want a centered body, 680px wide, having a red bottom margin. The following works well in galeon, but not in IE5, where the bottom margin steals the full width of the viewpoint. That is, the red...
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
0
by: David Mitchell | last post by:
Hello, Here is a very basic question, but it is frustrating me to no end nonetheless. I have one file called addLink.py. In a method in this file I am trying to instantiate a class and call a...
0
by: jason | last post by:
i have classic ASP code that is calling a C# class library, which is wrapped for COM interop, and registered in the COM+ MMC. i have written 3 objects for the class library so far, and all three...
3
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
10
by: Schizoid Man | last post by:
I have two classes. The first one called Program contains the Main() method. This class ideally should be able to call a method called normDensity from another class NormalDistribution code...
8
by: blisspikle | last post by:
Can any Public class be inherited from? I installed some software on my pc and I can use it in my code, but I cannot seem to inherit from it. It was an executable that installed on my pc, I do not...
2
by: tron.thomas | last post by:
The following code will print a message only once: class PrintOnce: printOnce = True def __init__(self): if PrintOnce.printOnce: print 'Printing once.' PrintOnce.printOnce = False first =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.