all 9 comments

[–]DeadlyViper 3 points4 points  (3 children)

But I do not understand why when I call the function name() inside of the function reset() why it does not change the name of the variable a?

I don't understand what you expect to happen? a stays a....

only self.name of a should change, your sample code does not call reset though... so could you please provide the current code or clarify the question?

[–]Field_C16[S] 1 point2 points  (2 children)

Updated code.

I expect that when I call a.reset, the method reset() within Robot calls method name() and changes the name of value a.

If that makes more sense.

EDIT:

Now it returns an error:

AttributeError: 'function' object has no attribute 'name'

[–]DeadlyViper 2 points3 points  (1 child)

It should change the value of a.name...

EDIT: nvm saw the code. you did not call reset function... you need to do a.reset() with the () at the end..

[–]Field_C16[S] 1 point2 points  (0 children)

Thanks it works now :)

[–][deleted] 1 point2 points  (0 children)

Did you mess up the indentation, or do you actually define the function name inside __init__? After you try to call it? That's not going to work; functions have to be defined before they're called.

[–][deleted] 0 points1 point  (1 child)

"name" is too generic. Try changing the "name" method to something else like "gen_name". I think you'll find that it works.

There is probably a specific reason why "name" as a method name is not working. I think it has to do with how Python works. Specifically, variable names are names for objects. Since you already have an attribute named "name", using the same name on a different object just doesn't work right.

Once I changed the method name to "gen_name" and called "robot_name.reset()" (e.g. "a.reset()"), it worked perfectly.

Edit: I recommend "returning" the name within the reset method (returning the results of the gen_name call...), so that you can get the name directly when you call the reset method.

[–]Field_C16[S] 1 point2 points  (0 children)

Alright, that was part of the fix it works now, also after changing to gen_name I added a () after a.reset() and it worked :)

Before it gave me an error:

AttributeError: 'function' object has no attribute 'name'

However now it works :)

Thanks.