0
class exampleclass: 
 def meth01(self, name): 
 print self.name
obj1 = exampleclass()
obj1.meth01("James")

error message:

Traceback (most recent call last): File "<pyshell#5>", line 1, in
<module>
 obj1.meth01("James") File "<pyshell#3>", line 3, in meth01
 print self.name AttributeError: exampleclass instance has no attribute 'name'

So what have I done wrong to produce this error, the name is in the parameters And I tried to set what name was so that it could print?

bolt19
2,0734 gold badges34 silver badges44 bronze badges
asked Sep 7, 2014 at 13:29
3
  • 1
    You need to first set the self.name. Commented Sep 7, 2014 at 13:31
  • @Kasra Please don't add "please" and "thanks" etc. to questions. Commented Sep 7, 2014 at 13:32
  • @hjpotter92 Why? Surely because name is in the parameters and has yet to be defined I can when define it when an object is created? I'm trying to figure out the purpose of writing self.name = name if i were to write it. Commented Sep 7, 2014 at 13:34

4 Answers 4

2

You are printing self.name. This is a member variable of the class, not the input variable of the function meth01. In your function name refers to the input, and self.name refers to a member variable of the class. But you are not setting it.

Do this instead:

class ExampleClass:
 def meth01(self, name):
 print( name )

To understand what is going on, expand it like this:

class ExampleClass:
 def setName(self, name):
 self.name = name
 def meth01(self, name):
 print('Input variable name: ', name)
 print('Member variable name: ', self.name)
 ec = ExampleClass()
 ec.meth01('John') # This line will fail because ec.name hasn't been set yet.
 ec.setName('Jane')
 ec.meth01('John')
 # This will print:
 #('Input variable name: ', 'John')
 #('Member variable name: ', 'Jane')
answered Sep 7, 2014 at 13:31
Sign up to request clarification or add additional context in comments.

3 Comments

@Unapidra , member variable of the class? can you explain what that means? I am trying to understand why people write, self.name = name ? It makes no sense, what is self.name it seems to be a method because of the dot but its not ? also why do we need to put it inside name ? and is that the same "name" as the one in the parameter? me head is about to explode.
@Grey, are you familiar with Object Oriented Programming? A member variable is a variable that belongs to a class. In Python member variables and functions/methods are referred to by the dot. No parentheses so it is a variable.
The name and self.name are two completely different things! def meth01(self, not_a_name): self.name = not_a_name
1

You need to make your __init__ method

class exampleclass:
 def __init__(self, name):
 self.name = name
answered Sep 7, 2014 at 13:32

Comments

1

A variable name is part of the parameter list, but there's no name member value in the example class object itself (yet).

Think of what your code does, step by step. First it instantiates a new exampleclass object.

You then call obj1.meth01("James"). meth01 is passed with the values self = obj1 and name="James"

meth01 then tries to find a value stored as obj1.name, but none exists, since in this case name is local to the method meth01 and is not part of the object obj01 itself.

answered Sep 7, 2014 at 13:38

Comments

1

You need to make a constructor that is used to initialise an object.

Make your __init__ method like this.

class exampleclass:
 def __init__(self, name):
 self.name = name

NOTE: Declare the constructor as mentioned only.

You can understand self like this:

Self stores the address of the object from which the constructor or method is called. So self.name=name would result in assigning the data member of the object from which it is called to the variable name which is passed as parameter to the constructor.

answered Sep 7, 2014 at 13:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.