Skip to content

Instantly share code, notes, and snippets.

@zhenghao1
Created April 4, 2013 06:44
Show Gist options
  • Save zhenghao1/5308346 to your computer and use it in GitHub Desktop.
Save zhenghao1/5308346 to your computer and use it in GitHub Desktop.
Accessing self in class attribute
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