What is the difference between declaring data attributes inside or outside __init__
2017-10-17 12:16
507 查看
https://stackoverflow.com/questions/13844804/what-is-the-difference-between-declaring-data-attributes-inside-or-outside-ini
https://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes
I'm trying to get my head around OOP in Python and I'm a bit confused when it comes to declare variables within a class. Should I declare them inside of the
The following code works just fine:
But declaring the variables outside of the
In your first example you are defining instance attributes. In the second,class attributes.
Class attributes are shared between all instances of that class, where as instance attributes are "owned" by that particular instance.
Difference by example
To understand the differences let's use an example.
We'll define a class with instance attributes:
And one with class attributes:
And a function that prints out information about one of these objects:
Let's create 2
When we call
However, if we were to do exactly the same thing, but using instances of
And then call
So as you can see -
The reason you don't see this behaviour in the string variables is because you are actually assigning a new variable to an instance. In this case that reference is "owned" by the instance and not shared at the class level. To illustrate let's assign a new
list to things for
This results in:
https://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes
I'm trying to get my head around OOP in Python and I'm a bit confused when it comes to declare variables within a class. Should I declare them inside of the
__init__procedure or outside it? What's the difference?
The following code works just fine:
# Declaring variables within __init__ class MyClass: def __init__(self): country = "" city = "" def information(self): print "Hi! I'm from %s, (%s)"%(self.city,self.country) me = MyClass() me.country = "Spain" me.city = "Barcelona" me.information()
But declaring the variables outside of the
__init__procedure also works:
# Declaring variables outside of __init__ class MyClass: country = "" city = "" def information(self): print "Hi! I'm from %s, (%s)"%(self.city,self.country) me = MyClass() me.country = "Spain" me.city = "Barcelona" me.information()
In your first example you are defining instance attributes. In the second,class attributes.
Class attributes are shared between all instances of that class, where as instance attributes are "owned" by that particular instance.
Difference by example
To understand the differences let's use an example.
We'll define a class with instance attributes:
class MyClassOne: def __init__(self): self.country = "Spain" self.city = "Barcelona" self.things = []
And one with class attributes:
class MyClassTwo: country = "Spain" city = "Barcelona" things = []
And a function that prints out information about one of these objects:
def information(obj): print "I'm from {0}, ({1}). I own: {2}".format( obj.city, obj.country, ','.join(obj.things))
Let's create 2
MyClassOneobjects and change one to be Milan, and give Milan "something":
foo1 = MyClassOne() bar1 = MyClassOne() foo1.city = "Milan" foo1.country = "Italy" foo1.things.append("Something")
When we call
information()on the
foo1and
bar1we get the values you'd expect:
>>> information(foo1) I'm from Milan, (Italy). I own: Something >>> information(bar1) I'm from Barcelona, (Spain). I own:
However, if we were to do exactly the same thing, but using instances of
MyClassTwoyou'll see that the class attributes are shared between instances.
foo2 = MyClassTwo() bar2 = MyClassTwo() foo2.city = "Milan" foo2.country = "Italy" foo2.things.append("Something")
And then call
information()...
>>> information(foo2) I'm from Milan, (Italy). I own: Something >>> information(bar2) I'm from Barcelona, (Spain). I own: Something
So as you can see -
thingsis being shared between the instances.
thingsis a reference to a list that each instance has access to. So if you append to things from any instance that same list will be seen by all other instances.
The reason you don't see this behaviour in the string variables is because you are actually assigning a new variable to an instance. In this case that reference is "owned" by the instance and not shared at the class level. To illustrate let's assign a new
list to things for
bar2:
bar2.things = []
This results in:
>>> information(foo2) I'm from Milan, (Italy). I own: Something >>> information(bar2) I'm from Barcelona, (Spain). I own:
相关文章推荐
- What is the difference between Initializing and Assignment inside a constructor?
- What is the difference between initWithCoder:, initWithNibName:, and awakeFromNib?
- What is the difference between CDATA and PCDATA?
- What is the difference between data mining and machine learning?
- What is the difference between Constructor and ngOnInit?
- What is the difference between Simple, Advanced and Parameter data sets?
- What is the difference between XML and C or C ?
- What is the difference between wc -1 < mydata.dat and wc -1 < mydata.dat ?
- What is the difference between initWithCoder:, initWithNibName:, and awakeFromNib?
- 进程与线程的区别 What is the difference between process and thread
- What is the difference between objectForKey and valueForKey?
- What is the difference between .NET 1.1,2.0,3.0,3.5 and 4.0
- what is the difference between definition and declaration in c
- CareerCup What is the difference between a computers heap and it's stack?
- What is the difference between left join and left outer join?
- What is the difference between LWP and threads?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Multi-thread: What is the difference between OpenMP and MPI
- What is the difference between differed processing mode and interactive mode?
- What is the difference between char s[] and char *s?