Python Forum
Syntax to print ndarray member of class containing ndarray
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax to print ndarray member of class containing ndarray
#1
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

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'
Reply


Messages In This Thread
Syntax to print ndarray member of class containing ndarray - by pjfarley3 - Jul-08-2020, 05:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Class member become static Quasar999 1 716 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  print(data) is suddenly invalid syntax db042190 6 1,258 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,255 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,960 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  invalid syntax in my class CompleteNewb 2 1,952 Dec-13-2021, 09:39 AM
Last Post: Larz60+
  Tuple generator, and function/class syntax quazirfan 3 3,969 Aug-10-2021, 09:32 AM
Last Post: buran
  How can I pass&return ndarray between python and c++? JESuh 0 1,811 Mar-09-2021, 08:29 AM
Last Post: JESuh
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,707 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Invalid syntax on print function DoctorSmiles 2 2,848 Jul-12-2020, 07:39 PM
Last Post: DoctorSmiles
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 2,004 Jul-10-2020, 04:51 AM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020