I am attempting to access a attribute from an object I have created in a separate python file.
I have tried the following code:
print(self.GENOME[0][0].x)
where self.GENOME[0][0]
is the object memory address.
However, I get
AttributeError: 'set' object has no attribute 'x'
agent.py:
import neuron
#Creates an array of custom shape (3,4,3) and assigns unique object
#address
for ii in range(len(_Topology)):
_Genome[ii] = [{neuron.Neuron()} for i in range(_Topology[ii]+1)]
#Calls object variable
print(self.GENOME[0][0].x)
neuron.py:
class Neuron:
def __init__(self):
self.x = 50
_Genome[ii]
contains a list that contains a set of at least on Neuron
instance. Simplifying, you can make it like this:
>>> a = [{Neuron()} for _ in [1,2]]
>>> a
[{<__main__.Neuron object at 0x0000000002CAFF28>}, {<__main__.Neuron object at 0x0000000002CAFF60>}]
>>> q = [a]
>>> q
[[{<__main__.Neuron object at 0x0000000002CAFF28>}, {<__main__.Neuron object at 0x0000000002CAFF60>}]]
>>>
If you print _Genome
, it will look something like that - I'm assuming _Genome
is list-like | q
above should be analogous to _Genome
.
Indexing into it looks like this
>>> q[0]
[{<__main__.Neuron object at 0x0000000002CAFF28>}, {<__main__.Neuron object at 0x0000000002CAFF60>}]
>>> type(q[0])
<class 'list'>
>>> q[0][0]
{<__main__.Neuron object at 0x0000000002CAFF28>}
>>> type(q[0][0])
<class 'set'>
>>>
Set behaviour is well documented - just like most of Python.
One way to access the contents of a set is with a for loop
>>> for thing in q[0][0]:
print(thing.x)
50
>>>
Another way to access the contents of a set is with the pop()
method but this will remove an arbitrary item from the set. I don't think you really want this - you have no control over which item you get if there are more than one and the original set has one less item.
>>> x = [[{Neuron()},{Neuron}]]
>>> t = x[0][0].pop()
>>> t
<__main__.Neuron object at 0x0000000002C2F2E8>
>>> t.x
50
>>> x
[[set(), {<class '__main__.Neuron'>}]]
>>>
You could also make a list from the set and use indices to access the contents of the list.
>>> q
[[{<__main__.Neuron object at 0x0000000002CAFF28>}, {<__main__.Neuron object at 0x0000000002CAFF60>}]]
>>> z = list(q[0][0])
>>> z
[<__main__.Neuron object at 0x0000000002CAFF28>]
>>> z[0].x
50
>>>
All of that seems overly complicated and you would probably be better off changing the way you contain the Neuron
instances. I have no idea if this is feasible for you. Just dispense with the set containing a single instance:
>>> a = [Neuron() for _ in [1,2]]
>>> a
[<__main__.Neuron object at 0x0000000002C2FDD8>, <__main__.Neuron object at 0x0000000002CD00B8>]
>>> q = [a]
>>> q[0][0]
<__main__.Neuron object at 0x0000000002C2FDD8>
>>> type(q[0][0])
<class '__main__.Neuron'>
>>> q[0][0].x
50
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.