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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:31:51+00:00 2026-06-17T12:31:51+00:00

What would be the most convenient way to create a class which instances’ attributes

  • 0

What would be the most convenient way to create a class which instances’ attributes can’t be changed from outside the class (you could still get the value), so it’d be possible to call self.var = v inside the class’ methods, but not ClassObject().var = v outside of the class?

I’ve tried messing with __setattr__() but if I override it, the name attribute cannot be initiated in the __init__() method. Only way would be to override __setattr__() and use object.__setattr__(), which I am doing at the moment:

class MyClass(object):
    def __init__(self, name):
        object.__setattr__(self, "name", name)
    def my_method(self):
        object.__setattr__(self, "name", self.name + "+")
    def __setattr__(self, attr, value):
        raise Exception("Usage restricted")

Now this solution works, and it’s enough, but I was wondering if there’s even a better solution. The problem with this one is: I can still call object.__setattr__(MyClass("foo"), "name", "foo_name") from anywhere outside the class.

Is there any way to totally prevent setting the variable to anything from outside of the class?

EDIT: Stupid me not mentioning I’m not looking for property here, some of you already answered it, however it’s not enough for me since it will leave self._name changeable.

  • 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-17T12:31:51+00:00Added an answer on June 17, 2026 at 12:31 pm

    No, you cannot do this in pure python.

    You can use properties to mark your attributes as read-only though; using underscore-prefixed ‘private’ attributes instead:

    class Foo(object):
        def __init__(self, value):
            self._spam = value
    
        @property
        def spam(self):
            return self._spam
    

    The above code only specifies a getter for the property; Python will not let you set a value for Foo().spam now:

    >>> class Foo(object):
    ...     def __init__(self, value):
    ...         self._spam = value
    ...     @property
    ...     def spam(self):
    ...         return self._spam
    ... 
    >>> f = Foo('eggs')
    >>> f.spam
    'eggs'
    >>> f.spam = 'ham'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: can't set attribute
    

    Of course, you can still access the ‘private’ _spam attribute from outside:

    >>> f._spam
    'eggs'
    >>> f._spam = 'ham'
    >>> f.spam
    'ham'
    

    You could use the double underscore convention, where attribute names with __ at the start (but not at the end!) are renamed on compilation. This is not meant for making a attribute inaccessible from the outside, it’s intent is to protect an attribute from being overwritten by a subclass instead.

    class Foo(object):
        def __init__(self, value):
            self.__spam = value
    
        @property
        def spam(self):
            return self.__spam
    

    You can still access those attributes:

    >>> f = Foo('eggs')
    >>> f.spam
    'eggs'
    >>> f.__spam
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Foo' object has no attribute '__spam'
    >>> f._Foo__spam
    'eggs'
    >>> f._Foo__spam = 'ham'
    >>> f.spam
    'ham'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask what is the most suitable way to create List
The most convenient, Pythonic way to remove duplicates from a list is basically: mylist
What would be the most efficient way of counting the number of times a
What would be the most elegant and efficient way of finding/returning the first list
In a nutshell: what sort of applications would be benefit most from the dashboard
I have this string: B82V16814133260 what would be the most efficient way to get
I have a class that can be called either from a GUI or form
I have an application in which it would be very convenient for me to
I would like to create a simple drop down list with static values which
I'm looking for a convenient way to use jQuery to remove all classes from

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.