How to print out

Hi,

I am trying to understand how to pass one class to another. for example,

class Prof(MITPerson): 
 def __init__(self, familyName, firstName, rank): 
  MITPerson.__init__(self, familyName, firstName) 
  self.rank = rank 
  self.teaching = {} 
 def addTeaching(self, term, subj): 
  try: 
    self.teaching[term].append(subj) 
  except KeyError: 
    self.teaching[term] = [subj] 
 def getTeaching(self, term): 
  try: 
    return self.teaching[term] 
  except KeyError: 
    return None 
 def lecture(self,toWhom,something): 
  return self.say(toWhom,something + ' as it is obvious') 
 def say(self,toWhom,something): 
  if type(toWhom) == UG: 
    return MITPerson.say(self,toWhom,'I do not understand why you say ' + something) 
  elif type(toWhom) == Prof: 
    return MITPerson.say(self,toWhom,'I really liked your paper on ' + something) 
  else: 
    return self.lecture(something)

class Faculty(object): 
 def __init__(self): 
  self.names = [] 
  self.IDs = [] 
  self.members = [] 
  self.place = None 
  
 def add(self,who): 
  if type(who)!= Prof: raise TypeError('not a professor') 
  if who.getIdNum() in self.IDs: raise ValueError('duplicate ID') 

  self.names.append(who.familyName()) 
  self.IDs.append(who.getIdNum()) 
  self.members.append(who) 
  
 def getnames(self):
   return self.names
 def getmembers(self):
   return self.members

 def __iter__(self): 
  self.place = 0 
  return self 
 def __next__(self): 
  if self.place >= len(self.names): 
    raise StopIteration 
  self.place += 1 
  return self.members[self.place-1]

grimson = Prof('Grimson','Eric', 'Full') 
lozano = Prof('Lozano-Perez', 'Tomas', 'Full') 
guttag = Prof('Guttag', 'John', 'Full') 
barzilay = Prof('Barzilay', 'Regina', 'Associate') 
course6 = Faculty() 
course6.add(grimson) 
course6.add(lozano) 
course6.add(guttag) 
course6.add(barzilay) 

It looks like the whole Prof class is passed on to Faculty class and saved in a var called “member”. I’m curious how data is stored in that variable. Is there a way to print out member?

I tried
print(course6.getmembers())
output is: [<main.Prof object at 0x7fee6a1d89d0>, <main.Prof object at 0x7fee6a1d8790>, <main.Prof object at 0x7fee6a1d8dc0>, <main.Prof object at 0x7fee6a1d8070>]


You can to implement __repr__ or __str__ on the Prof class so that print will have a more useful string to print.

Why do you hate us so much that you are torturing us with single-space indents in your code?

1 Like