简体   繁体   中英

How can I reuse functions across classes

I have a few functions I'd like to reuse. This isn't my EXACT example but it is functionally similar.

For example, we'll say I have a bunch of python lists of integers that live in different classes that are each their own file. ListClassA.py, ListClassB.py, ListClassC.py.

I want to get averages for all those classes. I could make a nice little function in every single class that says sum the list and divide it by the length. What I'd like to do is make a new class that can do all my nice little list functions (average, max value, etc) so I can just call listFunctions.average(list). How can I call my new class from my existing classes?

You can import the new class

from new_file import listFunctions

Then you can use listFunctions.average(list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM