Python - use list of classes as parameter in function

≡放荡痞女 提交于 2020-06-27 16:20:33

问题


I am learning python(3) after using C# for several years. Therefore i am coding some home project in python but i encountered some issue i cant seem to figuere out how to do in python.

Lets say i have a simple class like this:

class SomeItem:
    def __init__(self, var1, var2, var3):
        # Instance Variable
        self.var1= var1
        self.var2= var2
        self.var3 = var3

At some point in my code i am using this class to put a bunch of them in a list;

list_of_SomeItems = []
list_of_SomeItems.append(SomeItem("apple","banana", "pear"))
list_of_SomeItems.append(SomeItem("fruit","candy", "drinks"))
# so on and so forth
return list_of_SomeItems

But now comes the part i cant seem to figure out At a certain point i want to make a function that takes a list of these SomeItems and do somtehing with is.

def name_of_function(interval=1.0, someItemsList):
    for item in someItemsList:
        print(item.var1)

but obvious it wouldn't know at this point that item has a var1 as i havnt told the function that the list must be of SomeItem objects.

What is the proper way to declare the list in my function and associate that parameter with a class?

EDIT fixed; as @chepner and @Perfect mentioned the mistake was the optional parameter was put first (plain syntax error); after fixing that error; it worked as desired.


回答1:


The code will work as is after you fix the syntax errors.

Python is dynamically typed, you can just try foo.bar, and it will succeed if foo has the bar attribute, and if it doesn't it will just raise an AttributeError which you can catch with except and move on.




回答2:


If you are on python 3.5 and above, you can use the typing module to pass data type of the parameters. You can do this the following way:

from typing import List

class Item:
    def __init__(self, var1: str, var2: str, var3: str):
        self.var1: str = var1
        self.var2: str = var2
        self.var3: str = var3

itemlist = [Item("apple","banana", "pear"),Item("fruit","candy", "drinks")]

Function returns var1 from list of Item class instances

def get_var_one(items: List[Item])-> List[str]:
    return [elem.var1 for elem in items]

You can use the library mypy to perform static type check on your code.




回答3:


it wouldn't know at this point that item has a var1 as I haven't told the function that the list must be of SomeItem objects.

That's likely a design problem. When you create a list of things, they should generally have something in common -- that's why they're on the same list. Why would you put items on the list that don't have a var1 attribute, if you're later going to try to access that attribute?

It is possible to create heterogeneous lists and use conditional code and exception handling, but it often reflects confusion in the design.



来源:https://stackoverflow.com/questions/62435213/python-use-list-of-classes-as-parameter-in-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!