473,503 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cannot find object instance

hi,
i am a python newbie..while trying out some message passing between
two object instances i came across a problem
moduleA.py
--------------------
import moduleB
class MyClassA:
def __init__(self):
self.age=0
self.address=''
def createClassB(self):
self.objB=moduleB.MyClassB()
def validate(self,age,address):
if(age >= 100 or address==''):
self.age=20;
self.address="Broadway,NewYork"
self.objB.display(self.age,self.address)
def callB(self):
self.objB.execute()

if __name__ == "__main__":
objA=MyClassA()
objA.createClassB()
objA.callB()

moduleB.py
------------------
import moduleA

class MyClassB:
def __init__(self):
self.createA()
def createA(self):
self.objA=moduleA.MyClassA()
def execute(self):
self.objA.validate(111,'')
def display(self,age,address):
print 'displaying:',str(age),address

when i run the code i get a message
AttributeError: MyClassA instance has no attribute 'objB'

Do i have to put a placeholder for objB in __init__ of MyClassA ?
is that why i get this error?someone please tell me how i can solve
this? I tried to put self.objB=None but it didn't work..(i was
thinking of java style objB=NULL )

please help
thanks
jim
Aug 28 '08 #1
3 1289
jimgardener a écrit :
hi,
i am a python newbie..
From what I can see from your code, you're a newbie to programming in
general...
while trying out some message passing between
two object instances i came across a problem
First point : you have both a circular dependency between ModuleA and
ModuleB - which is better to avoid unless you really like headaches...
You mention Java somewhere else, so I assume you have some at least some
Java background. Point here is that Python is not Java, and doesn't
restrict you to one ("public") class per module. IOW, if you have two
strongly related classes (which is obviously the case here), better to
put both in a same module.
moduleA.py
--------------------
import moduleB
class MyClassA:
def __init__(self):
self.age=0
self.address=''
def createClassB(self):
self.objB=moduleB.MyClassB()
Why is this in a distinct method instead of being in the initializer ?
And why is it named create*Class* when it creates an *instance* ?
def validate(self,age,address):
if(age >= 100 or address==''):
self.age=20;
self.address="Broadway,NewYork"
self.objB.display(self.age,self.address)
def callB(self):
self.objB.execute()
Looks like a somewhat messy, circonvoluted and arbitrary
overcomplexificated control flow...
if __name__ == "__main__":
objA=MyClassA()
objA.createClassB()
objA.callB()

moduleB.py
------------------
import moduleA

class MyClassB:
def __init__(self):
self.createA()
def createA(self):
self.objA=moduleA.MyClassA()
You do understand that self.objA won't be the same MyClassA instance as
the one that instanciated this instance of MyClassB, do you ?
def execute(self):
self.objA.validate(111,'')
def display(self,age,address):
print 'displaying:',str(age),address
You don't have to call str on age here.
>
when i run the code i get a message
AttributeError: MyClassA instance has no attribute 'objB'
Indeed.

You first create an instance of MyClassA named objA, call createClassB()
on it which create an instance of MyClassB referenced by objA.objB.

When objA.objB is created, it itself creates another instance of
MyClassA referenced by objA.objB.objA, which is totally distinct from
objA...

Then you call objA.callB(),
which itself call objA.objB.execute(),
which in turn call objA.objB.objA.validate(),
which in turn call objA.objB.objA.objB.display().

But at this point, objA.objB.objA (which, I repeat, is *not* objA) has
no attribute objB since you didn't call objA.objB.objA.createClassB()...

You could have understood this by yourself, either the way I did
(mentally tracing program execution) or using a couple print statements
in your code (to effectively trace program execution), or using the
debugger...
Do i have to put a placeholder for objB in __init__ of MyClassA ?
is that why i get this error?someone please tell me how i can solve
this? I tried to put self.objB=None but it didn't work..
<ot>
Trying anything that comes to mind instead of trying to understand how
things work is a well-known antipattern named "programming by accident".
The best you can hope from this approach is that, for unknown reasons,
things accidentally seem to work. Until the day where, for still unknown
reasons, they either stop working, or it appears they never really
worked at all.

Needless to say, this approach is a pure waste of time.
</ot>

class ClassA(object):
def __init__(self, age=0, address=''):
self.age = age
self.address = address
self.b= ClassB(self)

def validate(self,age,address):
if(age >= 100 or address==''):
self.age=20;
self.address="Broadway,NewYork"
self.b.display(self.age, self.address)

def call_b(self):
self.b.execute()

class ClassB(object):
def __init__(self, a):
self.a = a
def execute(self):
self.a.validate(111,'')
def display(self, age, address):
print 'displaying: %s %s' % (age, address)

if __name__ == "__main__":
objA=ClassA()
objA.call_b()
Note that this is still uselessly messy and arbitrary
overcomplificated... Such a ping-pong between two classes is more often
than not a design smell.

HTH
Aug 28 '08 #2
On Thu, 28 Aug 2008 08:35:07 -0700, jimgardener wrote:
moduleA.py
--------------------
import moduleB

[…]

moduleB.py
------------------
import moduleA

[…]
Don't do that. Circular imports are a code smell. If two modules are
coupled that tight it's usually a sign that you want just one module or
factoring out something into a third module that is imported by both old
modules.

Ciao,
Marc 'BlackJack' Rintsch
Aug 28 '08 #3
On Aug 28, 9:35 pm, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Such a ping-pong between two classes is more often
than not a design smell.

thanks Bruno,i understand my mistake..should have figured it out
myself
thanks again
jim

Aug 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2025
by: Erlend Fuglum | last post by:
I have tried and tried, but cannot figure out the source of the following error: AttributeError: 'module' object has no attribute 'menyHMTL' __doc__ = 'Attribute not found.' __getitem__ =...
6
10306
by: Christopher Brandsdal | last post by:
Hi! I get an error when I run my code Is there any other way to get te information from my form? Heres the error I get and the code beneath. Line 120 is market with ''''''''''''Line...
1
1831
by: tskelley | last post by:
Something strange has happened on my computer. I am unable to link tables from SQL Server 2000. I have tried from Access 97 and 2000, but keep getting the error "The Microsoft Jet database engine...
0
2146
by: mschep | last post by:
Hi, I built an assembly with a set of user controls. This can be done with the Visual Studio 2005 Deployment Project: building and merging for example all your aspx and ascx in one dll (lets...
8
6234
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
5
2586
by: jm.suresh | last post by:
Hi I have three objects, all of them are instances of classes derived from a base class. Now, given one of the instance, I want to find the closest relative of the other two. How can I do this? ...
0
1110
by: aoztuncer | last post by:
I can import data from Excel using the following code if the spreadsheet is saved on the computer and has a non-empty path name: string connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +...
2
6637
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
8
20392
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
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...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.