Please help me understand this. I created a really simple program to try to understand classes.
class One(object):
def __init__(self, class2):
s
You are overwriting your name
attribute with method called name
. Just rename something.
The class Two
has an instance method name()
. So Two.name
refers to this method and the following code works fine:
Polly = Two()
Two.name(Polly)
However in __init__()
, you override name
by setting it to a string, so any time you create a new instance of Two
, the name
attribute will refer to the string instead of the function. This is why the following fails:
Polly = Two() # Polly.name is now the string 'Polly'
Polly.name() # this is equivalent to 'Polly'()
Just make sure you are using separate variable names for your methods and your instance variables.
I would strongly suggest you get into the habit of carefully naming things. As you can see, even with very small pieces of code, you can get into trouble. You'll definitely want to read the Python style PEP very carefully. http://www.python.org/dev/peps/pep-0008/
You have both a variable and a function called "name" in Two. Name one of them differently and it should work.