0

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.

2 Answers 2

2

@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).

Sign up to request clarification or add additional context in comments.

Comments

0

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

8 Comments

Yes I realise that. My question is about importing the namespace of the instance so that line in the example is considered valid code.
You don't need to import self, the code runs fine once you make that change to it. Your class function already has access to the the class attribute.
This question isn't about how to make the example work as simply as possible - the question is about how to import the attribute namespace of an instance. In some circumstances it would make the code a lot clearer without heaps of references to self.something throughout it.
I guess I am confused why you would want to do that? You don't need to import anything, its already there. If you wanted to, you could assign the name variable to self.name... but that's redundant. You could also pass in a variable to the function? function mangle_name(self, name) and then call it like a_name.mangle_name(a_name.name)... but that's really confusing...
Because a + b + c + d + e + f + g + h + ... is a lot clearer than self.a + self.b + self.c + self.d + self.e + self.f + self.g + self.h + .... Also, it can potentially save you having to alter code that is known to be working in the process of making some script into a class.
|

Your Answer

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.