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
#7
At line 19, I think I understand the logic of the first half: The method, __str__, returns a line break for each variable inside the template list, while also invoking the join function. Question #1: Is that correct or almost correct?

The second half of line 19 throws me off. How does the format function multiply the class variables exponentially (with ‘**’)? This question I’ve asked is sort of my feeble attempt at explaining it as best I can. Question #2: Can someone please explain the logic of the second half of line 19 of Mekire’s improved script?

Line 23 of Mekire’s script assigns the instances of all the class variables for the PassPort class. Question #3: If I wanted to now prompt the user in in my shell to ask for each variable, for example asking for the user’s first name to input into the first class variable instance, how do I do that? Here is my new script where I attempt to implement that feature (I am referring to line 23) :
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)

    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)
	citizen256032 = PassPort(first_name=input("What is your first name?"),'Ross', 'Dobbs', 'male', 1947, 44)
    print(citizen256032)

if __name__ == "__main__":
    main()
In my initial script I attempted to assess the iQ variable using some methods. Question #4: If I wanted to print a note below the iQ class instance under the __str__ method to indicate the status of the client's iQ (like if it is average, below average or genius), how could I do that better (than my attempt in my original post)?

Thanks for your attention.
Reply


Messages In This Thread
RE: Troubleshooting simple script and printing class objects - by Drone4four - Dec-06-2017, 08:15 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