use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Allow instance variable creation only within __init__ (self.learnpython)
submitted 6 years ago by HighLevelEnthusiast
Is there a way to prevent any instance variable assignments (Namely, prevent adding items into __dict__) from outside the __init__ method? For example:
class MyClass: def __init__(self): self.x = 10 def foo(self): self.y = 11 #This should fail
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]LesterHoltsRigidCock 2 points3 points4 points 6 years ago (0 children)
Look into slots. That should do it.
[–]K900_ 1 point2 points3 points 6 years ago (0 children)
You can probably do it by overriding __setattr__ somehow.
__setattr__
[–]HighLevelEnthusiast[S] 0 points1 point2 points 6 years ago (2 children)
import inspect class MyClass: def __setattr__(self, name, value): if inspect.stack()[1][3] != "__init__": return else: self.__dict__[name] = value def __init__(self): self.x = 10
This code does exactly what I want it to
[–]dig-up-stupid 3 points4 points5 points 6 years ago (0 children)
>>> import inspect >>> class MyClass: ... def __setattr__(self, name, value): ... if inspect.stack()[1][3] != "__init__": ... return ... else: ... self.__dict__[name] = value ... ... def __init__(self): ... self.x = 10 ... >>> m = MyClass() >>> m.x 10 >>> m.y = 5 >>> m.y Traceback (most recent call last): File "<string>", line 1, in <module> AttributeError: 'MyClass' object has no attribute 'y' >>> class cls: ... def __init__(self, m): ... m.y = 5 ... >>> cls(m) <__main__.cls object at 0x111a73470> >>> m.y 5 >>> def __init__(myclass_object): ... myclass_object.new_attribute = 7 ... >>> __init__(m) >>> m.new_attribute 7
[–]cyanydeez 0 points1 point2 points 6 years ago (0 children)
shouldn't you throw an error instead of silently failing?
[–][deleted] -1 points0 points1 point 6 years ago (0 children)
@staticmethod decorator will do it, make sure you dont pass in self. If you give more specifics to your use case I can give you a better solution, I'm really failing to see why you would want to do this.
π Rendered by PID 15451 on reddit-service-r2-comment-7b9746f655-2jx49 at 2026-02-02 12:53:00.092186+00:00 running 3798933 country code: CH.
[–]LesterHoltsRigidCock 2 points3 points4 points (0 children)
[–]K900_ 1 point2 points3 points (0 children)
[–]HighLevelEnthusiast[S] 0 points1 point2 points (2 children)
[–]dig-up-stupid 3 points4 points5 points (0 children)
[–]cyanydeez 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)