user18991295
user18991295

Reputation:

Can you create a list of class objects and use class functions that way?

I am trying to create a list of class objects, so that I can access functions and properties of a class/object by refering to that list. I looks something like this:

class Modul:
   def __init__(self, name, dozent, time):
        self.name = name
        self.dozent = dozent
        self.time = time
        listofmodules.append(self) #i also tried what will be in the main marked with *option instead

   def __str__(self):
      return self.name + "at" + self.time "tutored by" + self.dozent

   def a_function(self):
      #do something

if '__main__':
   listofmodules = []
   name = "subject" 
   dozent = "tutor" 
   time = "2 hrs"
   new_module = Modul(name, dozent, time)
   listofmodules.append(Modul(name, dozent, time)) #*option

I would like to do things like:

to_use = input("What module would you like to use?")
listofmodules.index(to_use)
listofmodules[index].a_function

Is this even possible at all? If yes, why does it save the str in the list? How do I make it not save the string but the module instead? Thanks in advance :)

Upvotes: 0

Views: 1009

Answers (1)

AiOverflow
AiOverflow

Reputation: 1

Your code doesn't quite make sense, because every time you create a new Modul you're appending self to listofmodules. This means that you'll get a growing list of objects - this is probably not the desired effect! Using the "Bob at 15 tutored by Alice" form is not a reliable way to refer to the module - you should use id! list.append can only add objects at the end of a list - use list.index to search for a given object. You want to prefer casing conventions - normally you'd write MyObject. a_function should probably just be self.a_function - I guess that you don't need the self

listofmodules.append(Modul(name, dozent, time))

This will create a new Modul, with the "Bob at 15 tutored by Alice" name, and append it to the list. This will create a new instance, however if you're trying to save the same one twice, you don't need to do anything.

my_modul = Modul(name, dozent, time)
listofmodules.append(my_modul)

# ...

to_use = input(What module would you like to use?)

for item in listofmodules:
  if str(item) == to_use:
    item.a_function()
    break
else:
  print("Didn't find given module!")
to_use = input(What module would you like to use?)
listofmodules.index(to_use)
listofmodules[index].a_function

This does not work, and will likely result in an error for list.index. list.index will search for a matching object - since you're modifying the str representation of the object when you're asking for it, this will not work. Alternatively, we can add an attribute to the module:

class Modul:
  def __init__(self, name, dozent, time, use_for_identity=None):
        self.name = name
        self.dozent = dozent
        self.time = time
        self.use_for_identify = use_for_identity or name

  def __str__(self):
    return self.name + at + self.time tutored by + self.dozent
  def a_function(self):
    #do something

Now we can do:

my_modul = Modul(name, dozent, time, use_for_identity="cool")
listofmodules.append(my_modul)

# ...

to_use = input(What module would you like to use?)

for item in listofmodules:
  if item.use_for_identify == to_use:
    item.a_function()
    break
else:
  print("Didn't find given module!")

Upvotes: 1

Related Questions