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
#3
So first and foremost, the problem with your program is a missing .

You wrote something like this:
>>> "Some string: {}"format("spam")
  File "<stdin>", line 1
    "Some string: {}"format("spam")
                          ^
SyntaxError: invalid syntax
>>>
Note the missing dot.
>>> "Some string: {}".format("spam")
'Some string: spam'
>>>
More than this though, the reason this error is so hard to see is the length of your lines.

Let's instead create a __str__ method for your class so we can print it directly.  Let's also write the print line a little more sanely:
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 # (Inteligence Quotient)

    def __str__(self):
        template = ["The citizen's first name is {first_name}.",
            "The citizen's middle name is {middle_name}.",
            "The citizen's last name is {last_name}.",
            "The species of the citizen is {species}.",
            "The gender of the citizen is {gender}.",
            "The citizen was born in the year {YOB}.",
            "And finally, the citizen has an iQ of {iQ}."]
        return "\n".join(template).format(**vars(self))


def main():
    citizen256032 = PassPort('Jon', 'Ross', 'Dobbs', 'male', 1947, 44)
    print(citizen256032)
 

if __name__ == "__main__":
    main()
Note now we can now just directly say print(citizen256032).
Reply


Messages In This Thread
RE: Troubleshooting simple script and printing class objects - by Mekire - Nov-05-2017, 12:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 305 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  OBS Script Troubleshooting Jotatochips 0 305 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,021 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Troubleshooting with PySpy Positron79 0 810 Jul-24-2022, 03:22 PM
Last Post: Positron79
  Simple Python script, path not defined dubinaone 3 2,713 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Troubleshooting site packages ('h5py' to be specific) aukhare 2 2,008 Nov-02-2020, 03:04 PM
Last Post: snippsat
  Class objects Python_User 12 4,481 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,055 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  OOP hellp (simple class person) onit 4 2,563 Jul-14-2020, 06:54 PM
Last Post: onit
  Need help creating a simple script Nonameface 12 4,608 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