-1

I was recently given a calling in my church and I'm trying to write a small program to help me keep track of some tasks. I have written some class code, and it prints, but I want each of these details to print either on their own line or with an overhead description. I'm not sure how to format this. Here is the code.

class Ministers:
    def __init__(self, last, first, middle, phone, email, district, 
    districtPresident, partner, interviewed, dateOfInterview):
        self.last = last
        self.first = first
        self.middle = middle
        self.phone = phone
        self.email = email
        self.district = district
        self.districtPresident = districtPresident
        self.partner = partner
        self.interviewed = interviewed
        self.dateOfInterview = dateOfInterview

    def info(self):
        return '{} {} {} {} {} {} {} {} {} {}'.format(self.last, self.first, 
        self.middle, self.phone, self.email, self.district, 
        self.districtPresident, self.partner, self.interviewed, 
        self.dateOfInterview)

minister_1 = Ministers('Arthur', 'Leslie', 'Brainard', 'unknown', 'unknown', 
'Central', 'Sean D. Burnham', 'Ben Pearson', 'n/a','n/a')

print(Ministers.info(minister_1))

While I'm at it, I may as well ask: is putting this many attributes to a class instance a conventional way of doing things? Should I have written this differently? Obviously I'm a newb.

The code prints like this:

last name first name middle name etc.

I'd like it to print like this:

last name
first name
middle name
etc

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Andy Meza III
  • 33
  • 1
  • 6
  • 3
    Use newlines in your format string? Anyway, this amount of attributes seem within the normal bounds to me. A style tip: name your class `Minister` not `Ministers`. Each instance will represent a single "minister". A class called `Ministers` implies that it is a specialized *container of ministers*. Also, don't do: `Ministers.info(minister_1)`, that defeats the purpose, use `minister_1.info()` – juanpa.arrivillaga Feb 19 '19 at 22:01

4 Answers4

1

A shorter way of achieving your output (only for Python 3.7+, as dicts are guaranteed to be insertion ordered here):

def info(self):
    return '\n'.join(self.__dict__.values())

Personally, I would get rid of the info function, as this the __str__ dunder method exists specifically for this purpose. So when you replace your function with:

def __str__(self):
    return '\n'.join(self.__dict__.values())

you can simply view your instance of Minister with print(minister_1).

DocDriven
  • 3,726
  • 6
  • 24
  • 53
  • This may not be in the order you want: `return '\n'.join(self.__dict__.values())` – juanpa.arrivillaga Feb 19 '19 at 22:25
  • @juanpa.arrivillaga: You are right, but after testing this several times, ``__dict__`` seems to preserve the order of the instance vars somehow. Is this intended? – DocDriven Feb 19 '19 at 22:32
  • It depends on what Python version you are using. In Python 3.7+, dict objects now maintain insertion order as a part of the language spec. In other versions, the order is an implementation detail, and `dict` objects are inherently unordered – juanpa.arrivillaga Feb 19 '19 at 22:34
  • @juanpa.arrivillaga: I am using Python 3.6, and according to [this thread](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6), dicts are insertion ordered in this version. So this solution may be correct if the OP is using 3.6+. – DocDriven Feb 19 '19 at 22:40
  • In 3.6 that is an implementation detail, not a guarantee – juanpa.arrivillaga Feb 19 '19 at 22:40
  • @juanpa.arrivillaga: I will add this information in my post, as we do not know the version he is actually using. – DocDriven Feb 19 '19 at 22:41
  • I should have mentioned I am using 3.7.2 so dictionary values will maintain insertion order. I didn't even think of a dictionary though. Thanks guys! – Andy Meza III Feb 20 '19 at 00:01
  • I checked out this posts solution and I find it the best one proposed, thanks everyone! Really appreciate the assistance for a new programmer! – Andy Meza III Feb 20 '19 at 00:10
0

Simply switch the format string to be separated by newlines instead of spaces, i.e. replace '{} {} {} {} {} {} {} {} {} {}' with '{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}'

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

you can use the format option that will be like:

$
f'{variable1}\n{variable2}.....'
$

and you don't need to put the format at the end

also you can unify your attributes in a dictionary so you can call all the at the same time maybe.

  • Well, the instance essentially acts like a dict of attributes, you can actually access this using the `self.__dict__` attribute, or `vars(self)`, however, the OP seems to want a particular order, in which case, just hard-coding that order is fine – juanpa.arrivillaga Feb 19 '19 at 22:26
0

Maybe not the most elegant way to solve the answer, but the following change to your return will do the trick. The "\n" is a new line feed.

return '{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}'.format(self.last, self.first,

Will_Roberts
  • 21
  • 1
  • 6