If I define a class Q containing a member which is an ndarray of strings, I can print that class member by name.
If I define another class G containing a member which is an ndarray of class Q objects, what syntax do I use to print all the actual contents of the contained class Q members and not "<__main__.Q object at ....>"?
Python 3 example and output follow.
TIA for any help you can provide to cure my ignorance.
Peter
If I define another class G containing a member which is an ndarray of class Q objects, what syntax do I use to print all the actual contents of the contained class Q members and not "<__main__.Q object at ....>"?
Python 3 example and output follow.
TIA for any help you can provide to cure my ignorance.
Peter
import numpy as np class Q: def __init__(self,qsize): self.qs = qsize self.s = np.full([qsize, qsize], " . ", dtype='<U3') class G: def __init__(self, gsize, gval): self.gs = gsize self.g = np.full([gsize, gsize, gsize], gval, dtype=Q) qsz = 2 qx = Q(qsz) print("Created gx = Q()") print(qsz, qx.qs, qx.s) gsz = 2 gx = G(gsz, qx) print("Created gx = G(qx)") print(gsz, gx.gs, gx.g) print(gsz, gx.gs, gx.g.s)Output:
Output:Created gx = Q()
2 2 [[' . ' ' . ']
[' . ' ' . ']]
Created gx = G(qx)
2 2 [[[<__main__.Q object at 0x000002538DD74CA0>
<__main__.Q object at 0x000002538DD74CA0>]
[<__main__.Q object at 0x000002538DD74CA0>
<__main__.Q object at 0x000002538DD74CA0>]]
[[<__main__.Q object at 0x000002538DD74CA0>
<__main__.Q object at 0x000002538DD74CA0>]
[<__main__.Q object at 0x000002538DD74CA0>
<__main__.Q object at 0x000002538DD74CA0>]]]
Traceback (most recent call last):
File "clstest.py", line 25, in <module>
print(gsz, gx.gs, gx.g.s)
AttributeError: 'numpy.ndarray' object has no attribute 's'