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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:57:19+00:00 2026-06-10T00:57:19+00:00

Say I want to create a class for car , tractor and boat .

  • 0

Say I want to create a class for car, tractor and boat. All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as an attribute of car and also the same instance in a list.

I cant track down any solid info on whether user defined classes are mutable and if there is a choice to choose when you define them, can anybody shed some light?

  • 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-10T00:57:20+00:00Added an answer on June 10, 2026 at 12:57 am

    User classes are considered mutable. Python doesn’t have (absolutely) private attributes, so you can always change a class by reaching into the internals.

    For using your class as a key in a dict or storing them in a set, you can define a .__hash__() method and a .__eq__() method, making a promise that your class is immutable. You generally design your class API to not mutate the internal state after creation in such cases.

    For example, if your engines are uniquely defined by their id, you can use that as the basis of your hash:

    class Engine(object):
        def __init__(self, id):
            self.id = id
    
        def __hash__(self):
            return hash(self.id)
    
        def __eq__(self, other):
            if isinstance(other, self.__class__):
                return self.id == other.id
            return NotImplemented
    

    Now you can use instances of class Engine in sets:

    >>> eng1 = Engine(1)
    >>> eng2 = Engine(2)
    >>> eng1 == eng2
    False
    >>> eng1 == eng1
    True
    >>> eng1 == Engine(1)
    True
    >>> engines = set([eng1, eng2])
    >>> engines
    set([<__main__.Engine object at 0x105ebef10>, <__main__.Engine object at 0x105ebef90>])
    >>> engines.add(Engine(1))
    >>> engines
    set([<__main__.Engine object at 0x105ebef10>, <__main__.Engine object at 0x105ebef90>])
    

    In the above sample I add another Engine(1) instance to the set, but it is recognized as already present and the set didn’t change.

    Note that as far as lists are concerned, the .__eq__() implementation is the important one; lists don’t care if an object is mutable or not, but with the .__eq__() method in place you can test if a given engine is already in a list:

    >>> Engine(1) in [eng1, eng2]
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding dependency injection. say i want to create a class
Let's say I create an instance of a class and want to assign some
Say I want to create a card class. and want to have enums for
I want to create a Class, say MyDiv , which inherits from the original
I want to create an object, let's say a Pie. class Pie def initialize(name,
Let's say I have created two objects from class foo and now want to
Let say I want to create an interface for a class that should be
Let's say I have a Car class in a game that can be played
Let us say that I want to create a class MyString which is a
Let's say I have the following class structure: class Car; class FooCar : public

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.