For example, this code is Python:
a = object()
a.b = 3
throws AttributeError: 'object' object has no attribute 'b'
But, this piece of code:
class c(object): pass
a = c()
a.b = 3
is just fine. Why can I assign property b, when class x does not have that property? How can I make my classes have only properties defined?
The
object
type is a built-in class written in C and doesn't let you add attributes to it. It has been expressly coded to prevent it.The easiest way to get the same behavior in your own classes is to use the
__slots__
attribute to define a list of the exact attributes you want to support. Python will reserve space for just those attributes and not allow any others.Of course, there are some caveats with this approach: you can't pickle such objects, and code that expects every object to have a
__dict__
attribute will break. A "more Pythonic" way would be to use a custom__setattr__()
as shown by another poster. Of course there are plenty of ways around that, and no way around setting__slots__
(aside from subclassing and adding your attributes to the subclass).In general, this is not something you should actually want to do in Python. If the user of your class wants to store some extra attributes on instances of the class, there's no reason not to let them, and in fact a lot of reasons why you might want to.
You can override the behavior of the
__setattr__
magic method like so.Of course, this will only prevent you from setting attributes like a.b (the dot form). You can still set the attributes using
a.__dict__[b]
= value. In that case, you should override the__dict__
method too.Creating an
object
instance has no features. Therefore setting attributes on an instance of a the baseobject
type is expressly disabled. You must subclass it to be able to create attributes.Hint: If you want a simple object to use as something on which to store properties, you can do so by creating an anonymous function with
lambda
. Functions, being objects, are able to store attributes as well, so this is perfectly legit:Python generally allows you to set any attribute on any object. This is a special case where the
object
class acts differently. There are also some modules implemented in C that act similarly.If you want your object to behave like this, you can define a
__setattr__(self, name, value)
method that explicitly does araise AttributeError()
if you try to set a member that's not on the "approved list" (see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916)This happens because when you say
a.b = 3
, it creates a variable in a that represents b. For example,returns
AttributeError: class a has no attribute b
However this code,
returns 3 as it sets the value of b in a, to 3.