Is there someway to import the attribute namespace of a class instance into a method?
Also, is there someway to direct output to the attribute namespace of the instance?
For example, consider this simple block of code:
class Name():
def __init__(self,name='Joe'):
self.name = name
def mangle_name(self):
# Magical python command like 'import self' goes here (I guess)
self.name = name + '123' # As expected, error here as 'name' is not defined
a_name = Name()
a_name.mangle_name()
print(a_name.name)
As expected, this block of code fails because name is not recognised. What I want to achieve is that the method mangle_name searches the attribute namespace of self to look for name (and therefore finding 'Joe' in the example). Is there some command such that the attribute namespace of self is imported, such that there will be no error and the output will be:
>>> Joe123
I understand that this is probably bad practice, but in some circumstances it would make the code a lot clearer without heaps of references to self.something throughout it. If this is instead 'so-terrible-and-never-should-be-done' practice, could you please explain why?
With regards to the second part of the question, what I want to do is:
class Name():
def __init__(self,name='Joe'):
self.name = name
def mangle_name(self):
# Magical python command like 'import self' goes here (I guess)
name = name + '123' # This line is different
a_name = Name()
a_name.mangle_name()
print(a_name.name)
Where this code returns the output:
>>> Joe123
Trying the command import self returns a module not found error.
I am using Python 3.5 and would therefore prefer a Python 3.5 compatible answer.
EDIT: For clarity.