python question about classes

D

diffuser78

I have following two classes
Code:

## file_A.py
class A(object):
## a contains is instances of A
a= [ ] def __init__(self, node):
self.nodes_in_A = []
self.nodes_in_A.append(node) packet_queue = [ ] ...etc

## file_B.py
import A
class B(object):
## n contains instances of B
n = [ ] def some_func(self):
_len = len (A.A.a[0].packet_queue)


I get an error AttributeError: 'A' object has no attribute
'packet_queue'


Can you please explain me ...why ? and How to fix this ?
 
D

Dennis Lee Bieber

I have following two classes

I sure hope your newsclient is responsible for this...

Python is white-space sensitive, but you've got "def" on the same
line as an assignment statement; you've got "packet_queue" initialized
on the same line as a list append...
Code:

## file_A.py
class A(object):
## a contains is instances of A

if "a" is supposed track the instances, you'll need to append stuff
to it.
a= [ ] def __init__(self, node):
self.nodes_in_A = []
self.nodes_in_A.append(node) packet_queue = [ ] ...etc

self.nodes_in_A is created for EACH instance of an A, so will only
have the one node passed in.

packet_queue is a local temporary variable that only exists during
the __init__ action... If it is supposed to be an instance attribute it
needs to be "self.packet_queue..."

## file_B.py
import A
class B(object):
## n contains instances of B
n = [ ] def some_func(self):
_len = len (A.A.a[0].packet_queue)

You never create an instance of A; you are trying to access an
element of an empty list, and you seem to believe that element has an
attribute.
Can you please explain me ...why ? and How to fix this ?

Can you explain just WHAT you are really trying to do? Not what the
error message is, but what you expect the stuff to perform.



--
 

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

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top