简体   繁体   中英

how can I extends from another class

I'm new in Python/Django and I'm working on a little project to improve my self, I would like to know how can I extends or how can I call a function from another class I already tried but I got some errors

this is my code: first class

# Create your views here.
class ContactCenter(object):
    def myFunction(self, logged_user_id = None):
         print("hello")

second class

from apps.contact.views import ContactCenter

class ListModelMixin(object):

    def list(self, request, *args, **kwargs):
        ContactCenter.myFunction()

You can do that in two ways, one subclass if from ContactCenter :

class ListModelMixin(ContactCenter):

    def list(self, request, *args, **kwargs):
        self.myFunction()

Or call the function from an instance of ContactCenter :

class ListModelMixin(object):

    def list(self, request, *args, **kwargs):
        instance = ContactCenter()
        instance.myFunction()

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