Created
April 4, 2013 06:44
-
-
Save zhenghao1/5308346 to your computer and use it in GitHub Desktop.
Accessing self in class attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A(object): | |
# Dictionary | |
extra_content = None | |
def get_context_data(self, **kwargs): | |
kwargs.update(extra_content) | |
return kwargs | |
class B(A): | |
extra_content = { | |
'user': self.request.user, # This will fail | |
'form': 'some form' | |
} | |
def get_context_data(self, **kwargs): | |
""" | |
The kwargs below should have a 'user' & 'form' key. | |
""" | |
kwargs = super(B, self).get_context_data(**kwargs) | |
return kwargs | |
The thing is I can't access the self variable above. I could change that to a string and use eval() to resolve it but | |
I'm wondering if there's a better solution. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment