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
  Metadata Xls file documnet properties Andrew_andy9642 0 989 Aug-29-2024, 04:46 PM
Last Post: Andrew_andy9642
  printing/out put issue with class arabuamir 3 1,647 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Printing out incidence values for Class Object SquderDragon 3 1,857 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  How can I access objects or widgets from one class in another class? Konstantin23 3 3,588 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  PDF properties doesn't show created or modified date Pedroski55 4 4,046 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How do I list properties quarinteen 0 1,850 May-01-2022, 04:15 PM
Last Post: quarinteen
  API design question: use methods or properties? johsmi96 1 2,644 Oct-12-2020, 02:24 PM
Last Post: buran
  Class objects Python_User 12 8,343 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 3,738 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Create a 3D volume with some properties. Rosendo 0 2,265 Jul-18-2020, 08:20 PM
Last Post: Rosendo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.