This question already has answers here:
Why can't you add attributes to object in python? [duplicate]
(2 answers)
Can't set attributes on instance of "object" class
(7 answers)
Closed 9 years ago.
I just realized that:
class A(object): pass
a = A()
a.x = 'whatever'
Works (does not raise an error and creates a new x member).
But this:
a = object()
a.x = 'whatever'
Raises:
AttributeError: 'object' object has no attribute 'x'
While I probably would never use this in real production code, I'm a bit curious about what the reason is for the different behaviors.
Any hints ?
Probably because of __slots__. By default your class have dict of all atributes which can be added to like in your first example. But that behaviour can bi overriden by using slots.
Also, some classes like datetime which are implemented in C also can not be extended with new attributes at runtime.
Workaround for such classes is to do something like :
class MyObject(): # extend that class, here we extend object
pass # add nothing to the class
o = MyObject()
o.x = 'whatever' # works
Related
This question already has answers here:
How does the #property decorator work in Python?
(15 answers)
How do Python properties work?
(4 answers)
Closed 4 years ago.
I can do
class Foo(object):
x = property(lambda _: 123)
f = Foo()
f.x
to get 123
However, if I try
p = property(lambda : 123)
p
I get
<property object at 0x108f2f3b8>
Now I understand that an member of a class instance is not the same thing as a regular variable but I'm not sure what exactly makes this behavior different.
Does the fact that you instantiate a class somehow do extra binding on property objects? Is it a special case or is it a behavior I can take advantage in other situations and extend? Related - are property objects useful outside of a class declaration? Or is it just for this specific case?
This question already has answers here:
What is the difference between class and instance attributes?
(5 answers)
Closed 5 years ago.
Came across one Python Class and I am finding it hard to understand how and why its working . A simplified example of the class is :
class Test:
def __init__(self):
self.var = 1
otherVar = 2
def myPrinter(self):
print self.__dict__ # Prints {'var': 1}
print self.var
print self.otherVar # Doubt !!
print self.__dict__ # Prints {'var': 1}
ob = Test()
ob.myPrinter()
My doubt is with the self.otherVar call not throwing an error while self.__dict__ does not show reference to otherVar
It's because otherVar is an attribute of the class, while the var you setup in the __init__ is an attribute of the instance.
The otherVar is visible to the instance, because python first tries to get the instance attribute values, if the instance doesn't have it, then it checks its class attributes. If you define a var in both with different values, things may get confusing at first.
Well, do you know that comparison that a class is like a blueprint and the instance is the object built following it, right? So, var is an extra you added while creating the instance.
If you want to see otherVar, do Test.__dict__. It won't show you var, but all the class attributes.
Play a little with it and with time you are going to get used to it. Class attributes may be tricky, but extremely useful.
otherVar is a class member, not instance member, that's why it doesn't show in __dict__.
It appears in self.__class__.__dict__. (this way doesn't work in Python 2.x)
By the way, otherVar member value is shared across all instances and also accessible from type object: Test.otherVar
Example here: https://trinket.io/python3/d245351e58
For a more in depth explanation check here
This question already has answers here:
How to copy a class?
(9 answers)
Closed 8 years ago.
In the example below, I am deepcopying a base class and messing with the attributes of the copy. Why is this messing with the attributes of the original class? Is there any way to avoid this? The current code structure I am working with can't support multiple inheritance, so I really want to be able to somehow copy the class.
>>> class Base(object):
... foo = "bar"
...
>>> base_copy = copy.deepcopy(Base)
>>> del base_copy.foo
>>>
>>> base_again = copy.deepcopy(Base)
>>> hasattr(base_again, 'foo')
False
>>>
copy() and deepcopy() are designed to copy objects not classes. When you pass a class the original class is returned.
Python documentation says :
This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.
Indeed when testing with your code we have:
In [8]: base_copy is Base
Out[8]: True
You could try messing with type() as suggested in the other answer but it's pretty hackish.
You could do
class B(A):
pass
It's the closest I can think of that would make a copy.
As stated in copy documentation:
This module does not copy types like module, method, stack trace,
stack frame, file, socket, window, array, or any similar types. It
does “copy” functions and classes (shallow and deeply), by returning
the original object unchanged
You can try to do it using metaclasses:
class Base(object):
foo = "bar"
# Create a new class called `base_copy` with attributes and base classes
# from Base without inheriting from Base
base_copy = type('base_copy', Base.__bases__, dict(Base.__dict__))
# Will delete attribute on `base_copy` class
del base_copy.foo
base_again = type('base_again', Base.__bases__, dict(Base.__dict__))
print(hasattr(base_again, 'foo')) # True
You can try using deepcopy on dict(Base.__dict__) object to copy objects like list, dict and others.
Define all variables inside the constructor.
You can create a new constructor which, on the basis of the argument provided, decides which all fields should exist in the object of the class.
Class Foo:
def __init__(self):
self.param0 = 0
self.param1 = 0
def __init__(self,i):
if i == 0:
self.param0 = 0
if i == 1:
self.param1 = 1
This question already has answers here:
How to avoid having class data shared among instances?
(7 answers)
Closed 9 years ago.
i just have a puzzling question abou class attributes in python.
consider the following class below:
class A:
__lst = []
def add(self, str):
self.__lst.append(str)
print len(self.__lst)
i tried to make two instances x and y and i got this:
>>> x = A()
>>> x.add('aaa')
1
>>> x.add('bbb')
2
>>> y = A()
>>> y.add('aaa')
3
>>> y.add('bbb')
4
i was expecting that the instance of y will have a separate copy of the list attribute but it seems that the list just gets bigger even if you use another object to add elements to the list. it's simply weird.
can anybody please enlighten me on this matter?
thanks a lot in advance for your help. :-)
If you define an attribute inside of the class body then it will be a class attribute, and it will be shared by all instances. In your code self.__lst is going to be a reference to A.__lst.
To have a separate list for each attribute, define it as self.__lst inside of the __init__() function:
class A(object):
def __init__(self):
self.__lst = []
def add(self, s):
self.__lst.append(s)
print len(self.__lst)
In addition to the change referenced above, I also made some minor modifications so that your code follows some Python best practices: inheriting from object (new-style class) and not using str (or any other built-in name) as a variable name.
Variables declared inside a class but not by means of self are class-level properties (like your __lst). They are equivalent to Java's static. If you want your property to be unique for all instances, you need to declare them via self (i.e., self.__lst).
This question already has answers here:
Usage of __slots__?
(14 answers)
Can't set attributes on instance of "object" class
(7 answers)
Closed 7 months ago.
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.
class c(object):
__slots__ = "foo", "bar", "baz"
a = c()
a.foo = 3 # works
a.b = 3 # AttributeError
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.
class C(object):
def __setattr__(self, name, value):
allowed_attrs = ('a', 'b', 'c')
if name not in allowed_attrs:
# raise exception
# or do something else
pass
self.__dict__[name] = value
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.
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 a raise 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)
Creating an object instance has no features. Therefore setting attributes on an instance of a the base object 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:
>>> a = lambda: None
>>> a.b = 3
>>> a.b
3
This happens because when you say a.b = 3, it creates a variable in a that represents b. For example,
class a: pass
print a.b
returns AttributeError: class a has no attribute b
However this code,
class a: pass
a.b = 3
print a.b
returns 3 as it sets the value of b in a, to 3.