Python Forum
printing class properties from numpy.ndarrays of objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing class properties from numpy.ndarrays of objects
#1
Another numpy ndarray question: If I have a class value as the value of each numpy.ndarray element, how do I print the values of the embedded objects?

Example code and console output follows. What I would like to see is the same kind of output from printing elements of g.q as I can print from g.s.

OS is Win10-64 Pro, Python version is 3.8.3.

#!/usr/bin/env python
import numpy as np

class Q:
    def __init__(self, qsize, sval):
        self.s = np.full([qsize], sval, dtype=(str,3))

class G:
    def __init__(self, gsize, qval):
        self.m = np.ones([gsize], np.int32)
        self.q = np.full([gsize], qval, dtype=Q)

q = Q(3, "abc")
for x in q.s:
    print(x)

g = G(3, q)

for x in g.m:
    print(x)

for x in g.q:
    print(x)

exit()
Output:
abc abc abc 1 1 1 <__main__.Q object at 0x000001E0960AACA0> <__main__.Q object at 0x000001E0960AACA0> <__main__.Q object at 0x000001E0960AACA0>
Thanks in advance for helping to cure more of my python ignorance.

Peter
Reply
#2
Correction: I have a typo in there. I should have said:

"... of g.q as I can print from q.s." not g.s.

Thanks for your patience.

Peter
Reply
#3
Never mind, I figured it out. Corrected code and output below.

Apologies for wasting bandwidth.

Peter

#!/usr/bin/env python
import numpy as np

class Q:
    def __init__(self, qsize, sval):
        self.s = np.full([qsize], sval, dtype=(str,3))

class G:
    def __init__(self, gsize, qval):
        self.m = np.ones([gsize], np.int32)
        self.q = np.full([gsize], qval, dtype=Q)

q = Q(3, "abc")
for x in q.s:
    print(x)

g = G(3, q)

for x in g.m:
    print(x)

for x in g.q:
    print(x.s)

exit()
Output:
abc abc abc 1 1 1 ['abc' 'abc' 'abc'] ['abc' 'abc' 'abc'] ['abc' 'abc' 'abc']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 282 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  How can I access objects or widgets from one class in another class? Konstantin23 3 999 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  PDF properties doesn't show created or modified date Pedroski55 4 1,076 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How do I list properties quarinteen 0 1,052 May-01-2022, 04:15 PM
Last Post: quarinteen
  API design question: use methods or properties? johsmi96 1 1,674 Oct-12-2020, 02:24 PM
Last Post: buran
  Class objects Python_User 12 4,439 Aug-27-2020, 08:02 PM
Last Post: Python_User
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,033 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Create a 3D volume with some properties. Rosendo 0 1,417 Jul-18-2020, 08:20 PM
Last Post: Rosendo
  filling and printing numpy arrays of str pjfarley3 4 3,293 Jun-07-2020, 09:09 PM
Last Post: pjfarley3
  adding properties to variables rudihammad 0 1,683 May-06-2020, 05:09 AM
Last Post: rudihammad

Forum Jump:

User Panel Messages

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