Reputation: 103
I am currently wondering why this modest piece of code has an output I'm not expecting:
class Product(object):
price = 0
def __init__(self, tmp_price):
self.price = tmp_price
class Market(object):
products = []
def __init__(self):
self.products.append(Product(10))
a = Market()
b = Market()
print a.products[0]
print b.products[0]
print len(a.products)
Indeed, I get some output like:
<__main__.Product object at 0x7fe5899e46d0>
<__main__.Product object at 0x7fe5899e46d0>
2
Does anyone have an explanation? I guess it has something to do with python's way to handle references and all but...
Upvotes: 2
Views: 134
Reputation: 55962
You are referencing static class variables.
Static class variables in Python THere is lots of information about it online!
Market.products[0]
is the actual value.
This is not the same as an instance variable as you found out. http://legacy.python.org/doc/essays/ppt/acm-ws/sld051.htm
Upvotes: 4
Reputation: 45542
The problem is in this part of your code:
class Market(object):
products = [] # products belongs to the class and is shared by all instances!
def __init__(self):
self.products.append(Product(10)) # appending to the 'products' class atribute
The products
attribute belongs to the class and is shared by all instances. Class attributes are accessible from all instances of the class. When you reference products from self (self.products
), Python does not find products
belonging to the instance, so then it looks in to see if products
can be found in the class.
What you really want is this:
class Market(object):
def __init__(self):
self.products = [] # products belongs to the instance
self.products.append(Product(10))
See also Python: Difference between class and instance attributes
Upvotes: 5
Reputation: 2198
Because in Market init, self.products is using Market class member products, it shared between all Market's instance.
Upvotes: 2
Reputation: 1101
That is the default way Python implements the _str_() function:
<module-name>.<class-name> <type> at <memory-address>
If you don't like this representation, you can always give your own implementation of _str_(). The memory-addresses are provided as a way for you to check if two variables actually point to the same item.
Upvotes: 1
Reputation: 17909
It's because you're printing the objects reference, not the value of its field;
Try changing
print a.products[0]
print b.products[0]
to
print a.products[0].price
print b.products[0].price
Upvotes: 3