Reputation: 469
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.
Upvotes: 0
Views: 75
Reputation: 55600
@martin-lear has correctly answered the second part of your question. To address the first part, I'd offer these observations:
Firstly, there is no language feature that lets you "import" the instance's namespace into the method scope, other than performing attribute lookups on self.
You could probably abuse exec
or eval
and the instance's __ dict __
to get something like what you want, but you'd have to manually dump the instance namespace into the method scope at the beginning of the method and then update the instance's __dict __
at the end.
Even if this can be done it should be avoided though. Source code is primarily a means of communication between humans, and if your code violates its readers' expectations in such a way it will be more difficult to understand and read, and the cost of this over time will outweigh the cost of you having to type self in front of instance variable names. The principle that you should code as if the future maintainers are psychopaths who know where you live applies here.
Finally, consider that the irritation of having to type self a lot is telling you something about your design. Perhaps having to type something like self.a + self.b + self.c + self.d + self.e + self.f + self.g + self.h + ....
is a sign that you are missing an abstraction - perhaps a collection in this case - and you should be looking to design your class in such a way that you only have to type sum(self.foos)
.
Upvotes: 2
Reputation: 272
So in your mangle_name function, you are getting the error becuase you have not defined the name variable
self.name = name + '123'
should be
self.name = self.name + '123'
or something similiar
Upvotes: 0