Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7991437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:17:24+00:00 2026-06-04T13:17:24+00:00

This question is about Python inheritance but is explained with a Django example, this

  • 0

This question is about Python inheritance but is explained with a Django example, this should’t hurt though.

I have this Django model, with Page and RichText models as well:

class Gallery(Page, RichText):
    def save(self, *args, **kwargs):
        # lot of code to unzip, check and create image instances.
        return "something"

I’m only interested in using the save method in another class.

A solution could be:

class MyGallery(models.Model):
    def save(self, *args, **kwargs):
        # here goes the code duplicated from Gallery, the same.
        return "something"

I’d like to avoid the code duplication and also I’m not interested in inheriting members from Page and RichText (so I don’t want to do class MyGallery(Gallery):. If it would be legal I’d write something like this:

class MyGallery(models.Model):
    # custom fields specific for MyGallery
    # name = models.CharField(max_length=50)
    # etc

    def save(self, *args, **kwargs):
        return Gallery.save(self, *args, **kwargs)

But it won’t work because the save() in Gallery expects an instance of Gallery, not MyGallery.

Any way to “detach” the save() method from Gallery and use it in MyGallery as it were defined there?

EDIT:

I forgot to say that Gallery is given and can’t be changed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:17:26+00:00Added an answer on June 4, 2026 at 1:17 pm

    You can access the __func__ attribute of the save method:

    class Gallery(object):
        def save(self, *args, **kwargs):
            return self, args, kwargs
    
    class MyGallery(object):
        def save(self, *args, **kwargs):
            return Gallery.save.__func__(self, *args, **kwargs)
        # or 
        # save = Gallery.save.__func__
    
    mg = MyGallery()
    print mg.save('arg', kwarg='kwarg')
    # (<__main__.MyGallery object at 0x04DAD070>, ('arg',), {'kwarg': 'kwarg'})
    

    but you’re better off refactoring if possible:

    class SaveMixin(object):
        def save(self, *args, **kwargs):
            return self, args, kwargs
    
    class Gallery(SaveMixin, object):
        pass
    
    class MyGallery(SaveMixin, object):
        pass
    

    or

    def gallery_save(self, *args, **kwargs):
        return self, args, kwargs
    
    class Gallery(object):
        save = gallery_save
    
    class MyGallery(object):
        save = gallery_save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked this question here about a Python command that fetches a URL
I have a question about regex/Python. Sorry if this topic has been discussed millions
This question is similar to this question about subtracting dates with Python , but
I've seen this question about advice for C# programmers writing Python code but I
I don't mean for this question to be about Python vs PHP but about
I have some questions about the performance of this simple python script: import sys,
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
I have seen this question about deploying to WebSphere using the WAS ant tasks.
JD Long helped me with this: question about manual annotation . But is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.