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

  • Home
  • SEARCH
  • 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 4331794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:09:50+00:00 2026-05-21T10:09:50+00:00

Let’s say there is a library function called get_pack() which returns a Pack object:

  • 0

Let’s say there is a library function called get_pack() which returns a Pack object:

class Pack(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight
        ...

What I want to do is to wrap this object into my object which is let’s say CommercialPack which has a couple of useful functions:

class CommercialPack(Pack):
    def get_delivery_price(self, distance):
        return self.weight * PER_KM_PRICE * distance
    ...

And then create a function which returns CommercialPack instead of Pack. In some other languages like Delphi or Java, you can type cast that object on the fly. So I want to have something like:

def get_commercial_pack():
    return CommercialPack(get_pack())

and then you have all you need with no hassle. Because I would like my CommercialPack object to have all the properties and functions that Pack object has. I just want to wrap it inside my new class. Basically I don’t want to do something like this:

class CommercialPack(object):
    def __init__(self, pack):
        self.name = pack.name
        self.weight = pack.weight
        ...

OR

class CommercialPack(object):
    def __init__(self, pack):
        self.pack = pack

I’m looking for an elegant solution instead like I said, some kind of type casting or whatever I can do elegantly in Python.

Thanks very much.

  • 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-21T10:09:51+00:00Added an answer on May 21, 2026 at 10:09 am

    Perhaps something like this

    class CommercialPack(object):
        def __init__(self, pack):
            self.__dict__.update(pack.__dict__)
    

    You can even do this if you don’t mind sharing state between the pack and the commercial pack

    class CommercialPack(object):
        def __init__(self, pack):
            self.__dict__ = pack.__dict__
    

    It should be ok in your example since you aren’t keeping any other references to the pack object

    eg.

    PER_KM_PRICE = 100
    
    class Pack(object):
        def __init__(self, name, weight):
            self.name = name
            self.weight = weight
    
    
    class CommercialPack(Pack):
        def __init__(self, pack):
            self.__dict__ = pack.__dict__
    
        def get_delivery_price(self, distance):
            return self.weight * PER_KM_PRICE * distance
    
    def get_pack():
        return Pack("pack", 20)
    
    cp = CommercialPack(get_pack())
    print cp.get_delivery_price(3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.