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 3963240
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:11:30+00:00 2026-05-20T03:11:30+00:00

I have a method on one of my objects that returns a new instance

  • 0

I have a method on one of my objects that returns a new instance of that same class. I’m trying to figure out the most idiomatic way to write this method such that it generates a new object of the same type without duplicating code.

Since this method uses data from the instance, my first pass is:

class Foo(object):
    def get_new(self):
        data = # Do interesting things
        return Foo(data)

However, if I subclass Foo and don’t override get_new, calling get_new on SubFoo would return a Foo! So, I could write a classmethod:

class Foo(object):

    @classmethod
    def get_new(cls, obj):
        data = # Munge about in objects internals
        return cls(data)

However, the data I’m accessing is specific to the object, so it seems to break encapsulation for this not to be a “normal” (undecorated) method. Additionally, you then have to call it like SubFoo.get_new(sub_foo_inst), which seems redundant. I’d like the object to just “know” which type to return — the same type as itself!

I suppose it’s also possible to add a factory method to the class, and override the return type everywhere, without duplicating the logic, but that seems to put a lot of work on the subclasses.

So, my question is, what’s the best way to write a method that gives flexibility in type of class without having to annotate the type all over the place?

  • 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-05-20T03:11:30+00:00Added an answer on May 20, 2026 at 3:11 am

    If you want to make it more flexible for subclassing, you can simply use the self.__class__ special attribute:

    class Foo(object):
        def __init__(self, data):
            self.data = data
    
        def get_new(self):
            data = # Do interesting things
            return self.__class__(data)
    

    Note that using the @classmethod approach will prevent you from accessing data within any one instance, removing it as a viable solution in instances where #Do interesting things relies on data stored within an instance.

    For Python 2, I do not recommend using type(self), as this will return an inappropriate value for classic classes (i.e., those not subclassed from the base object):

    >>> class Foo:
    ...     pass
    ... 
    >>> f = Foo()
    >>> type(f)
    <type 'instance'>
    >>> f.__class__    # Note that the __class__ attribute still works
    <class '__main__.Foo'>
    

    For Python 3, this is not as much of an issue, as all classes are derived from object, however, I believe self.__class__ is considered the more Pythonic idiom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller method that returns a jSON object and in one calling
I have one method that opens a file and passes off to another function
I have one method in my managed bean which returns javascript as a string.
I have a project with a lot of panels. I have one method that
I have a utility method (= a static one) that I call a lot,
I have a factory method that creates objects to be used in unit tests.
suppose I have a model and a view ,ths view have two method:one is
I have one method with one letter(string\char?) as argument and one word active at
I have one combobox on the window form and I have one method which
I have a create method in one controller and at the end of this

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.