Python Forum
Troubleshooting simple script and printing class objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubleshooting simple script and printing class objects
#10
Consider writing an instance method which returns the data. Just for printing convenience. 

class PassPort(object):
    def __init__(self, first_name, middle_name, last_name, gender, YOB, iQ):
        self.species = 'homo-sapien'
        self.first_name = first_name
        self.middle_name = middle_name
        self.last_name = last_name
        self.gender = gender
        self.YOB = YOB # (Year of birth)
        self.iQ = iQ # (Intelligence Quotient)
    
    # the property decorator will make the function accesible as if it is a class attribute
    @propert
    def data(self):
        return (self.first_name, self.middle_name, self.last_name, self.gender, self.YOB, self.iQ)

person = ['Jon','Ross','Dobbs','male',1947,44]
obj = PassPort(*person)
Then, when you want to print it you can just:
print("First:\t{}\n \
Middle:\t{}\n \
Last:\t{}\n \
Gender:\t{}\n \
Birth:\t{}\n \
IQ:\t{}".format(*obj.data)) # instead of typing each class attr
Output:
First:    Jon Middle:    Ross Last:    Dobbs Gender:    male Birth:    1947 IQ:    44
Well, the output looks like this. The formatting is lost somewhere:
[Image: hnvm5r]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: Troubleshooting simple script and printing class objects - by wavic - Dec-15-2017, 01:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 335 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  OBS Script Troubleshooting Jotatochips 0 320 Feb-10-2024, 06:18 PM
Last Post: Jotatochips
  How can I access objects or widgets from one class in another class? Konstantin23 3 1,073 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Troubleshooting with PySpy Positron79 0 832 Jul-24-2022, 03:22 PM
Last Post: Positron79
  Simple Python script, path not defined dubinaone 3 2,745 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Troubleshooting site packages ('h5py' to be specific) aukhare 2 2,036 Nov-02-2020, 03:04 PM
Last Post: snippsat
  Class objects Python_User 12 4,532 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,076 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  OOP hellp (simple class person) onit 4 2,594 Jul-14-2020, 06:54 PM
Last Post: onit
  Need help creating a simple script Nonameface 12 4,695 Jul-14-2020, 02:10 PM
Last Post: BitPythoner

Forum Jump:

User Panel Messages

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