So I'm learning about classes. Why can't I use the third block of code to do the same thing that the second block of code apparently does? Why do I have to assign p1 to person()and not just use person() in the way I do in the third block of code?
#class
class person:
def asdf(self):
self.firstname=""
self.lastname=""
self.id=""
self.email=""
self.friends=[]
#second block of code
p1 = person()
p1.firstname="Dave"
p1.lastname="Johnson"
p1.id="2345239"
p1.email="[email protected]"
print p1.firstname
#third block of code
person().firstname="Dave"
person().lastname="Johnson"
person().id="2345239"
person().email="[email protected]"
print person().firstname
-
1I think you need to re-read whatever tutorial it is that you're learning from.Karl Knechtel– Karl Knechtel2012年02月13日 20:53:25 +00:00Commented Feb 13, 2012 at 20:53
3 Answers 3
In the second block, you change the properties of the same instance.
p1 = person() # create new instance
p1.firstname="Dave" # change its first name
p1.lastname="Johnson" # change its last name
# ...
print p1.firstname # access its firstname
In the third block, you create a new instance in each line.
person().firstname="Dave" # create new instance and change its firstname
person().lastname="Johnson" # create new instance and change its lastname
# ...
print person().firstname # create new instance and access its firstname
To be more accurate, the problem only occurs in the last line, as you try to access an attirbute that was not yet declared, since the firstname attribute is declared only in the function asdf, or, in the second block, in the line p1.firstname="Dave"
Here's a simple example:
>>> class A:
... def AA(self):
... self.a = 1
...
>>> a = A()
>>> a.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute 'a'
>>> a.a = 1
>>> a.a
1
2 Comments
instance has no attribute... So creating p1 as an instance of person() tells python that it has these attributes such as firstname? I'm not sure what an attribute is exactly.p1 is a person, then p1.firstname is the firstname attribute of that person. You do not "create p1 as an instance of person()"; you assign the newly-created instance of the person class (which is what person() creates) to the variable p1. You get the error message from the third block because the person whose firstname you are checking is not the same person whose firstname you assigned. When you set p1's firstname and then check p1's firstname later, it's obviously the same person in both places.In the third block of code, each call to person() creates a new instance of the person class. The code then sets the value of an attribute on that object, then discards the entire object because it is not stored in any variable.
Comments
FYI, the python style is to use camel case on classes. This is part of the problem. The initial cap helps distinguish class construction from method calls.
Defining a class:
class Person(object):
def asdf(self):
self.firstname=""
self.lastname=""
self.id=""
self.email=""
self.friends=[]
Making an instance:
p = Person()
Using the class name like a function call constructs an instance, using the class as a prototype. In the second block, you've created an instance and you're changing its attributes.
In the third block, you've created four new instances of person, set one attribute on them, and thrown them away (because they're not assigned to anything).
The writeup on classes is really worth a read: http://docs.python.org/tutorial/classes.html
There's a fair amount of underlying implementation that you don't need, but there's also some critical details about classes, instances, and attributes.