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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:40:13+00:00 2026-05-25T20:40:13+00:00

Suppose I have a class: class Car(object): def __init__(self, name, tank_size=10, mpg=30): self.name =

  • 0

Suppose I have a class:

class Car(object):
    def __init__(self, name, tank_size=10, mpg=30):
        self.name = name
        self.tank_size = tank_size
        self.mpg = mpg

I put together a list of the cars I’m looking at:

cars = []
cars.append(Car("Toyota", 11, 29))
cars.append(Car("Ford", 15, 12))
cars.append(Car("Honda", 12, 25))

If I assign a name to my current favorite (a “pointer” into the list, if you will):

my_current_fav = cars[1]

I can easily access the attributes of my current favorite:

my_current_fav.name       # Returns "Ford"
my_current_fav.tank_size  # Returns 15
my_current_fav.mpg        # Returns 12

Here’s where I start getting foggy. I would like to provide additional “computed” attributes only for my current favorite (let’s assume these attributes are too “expensive” to store in the original list and are easier to just compute):

my_current_fav.range      # Would return tank_size * mpg = 180
                          # (if pointing to "Ford")

In my case, I just capitulated and added ‘range’ as an attribute of Car(). But what if storing ‘range’ in each Car() instance was expensive but calculating it was cheap?

I considered making ‘my_current_fav’ a sub-class of Car(), but I couldn’t figure out a way to do that and still maintain my ability to simply “point” ‘my_current_favorite’ to an entry in the ‘cars’ list.

I also considered using decorators to compute and return ‘range’, but couldn’t figure out a way to also provide access to the attributes ‘name’, ‘mpg’, etc.

Is there an elegant way to point to any item in the list ‘cars’, provide access to the attributes of the instance being pointed to as well as provide additional attributes not found in the class Car?

Additional information:
After reading many of your answers, I see there is background information I should have put into the original question. Rather than comment on many answers individually, I’ll put the additional info here.

This question is a simplification of a more complicated issue. The original problem involves modifications to an existing library. While making range a method call rather than an attribute is a good way to go, changing

some_var = my_current_favorite.range

to

some_var = my_current_favorite.range()

in many existing user scripts would be expensive. Heck, tracking down those user scripts would be expensive.

Likewise, my current approach of computing range for every car isn’t “expensive” in Python terms, but is expensive in run-time because the real-world analog requires slow calls to the (embedded) OS. While Python itself isn’t slow, those calls are, so I am seeking to minimize them.

  • 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-25T20:40:14+00:00Added an answer on May 25, 2026 at 8:40 pm

    This is easiest to do for your example, without changing Car, and changing as little else as possible, with __getattr__:

    class Car(object):
        def __init__(self, name, tank_size=10, mpg=30):
            self.name = name
            self.tank_size = tank_size
            self.mpg = mpg
    
    class Favorite(object):
        def __init__(self, car):
            self.car = car
        def __getattr__(self, attr):
            return getattr(self.car, attr)
        @property
        def range(self):
            return self.mpg * self.tank_size
    
    cars = []
    cars.append(Car("Toyota", 11, 29))
    cars.append(Car("Ford", 15, 12))
    cars.append(Car("Honda", 12, 25))
    
    my_current_fav = Favorite(cars[1])
    
    print my_current_fav.range
    

    Any attribute not found on an instance of Favorite will be looked up on the Favorite instances car attribute, which you set when you make the Favorite.

    Your example of range isn’t a particularly good one for something to add to Favorite, because it should just be a property of car, but I used it for simplicity.

    Edit: Note that a benefit of this method is if you change your favorite car, and you’ve not stored anything car-specific on Favorite, you can change the existing favorite to a different car with:

    my_current_fav.car = cars[0] # or Car('whatever')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: class myclass: def __init__(self): self.foo = bar where the value of
Suppose that I have a list of cars : public class Car { private
Suppose I have a class module clsMyClass with an object as a member variable.
I have a Car class with various properties. I have a static Cars collection
Suppose you have following class: class ProcessController { public List<Process> Active { get {
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
suppose i have a class engin and i inherit a class car from engin
Suppose you have the following class: class Car : IPainting { ... } Then
Suppose I have class A { public List<B> LiProperty { get; set { //will
Suppose I have a class like this: public class Car { private double distanceDriven;

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.