Calling function within a dict within a class

class Test(object):

    some_dictionary = {
        'key_one': self.method_one(),
        'key_two': self.method_two(),
        'key_three': self.method_three()
    }

'self' here fails because it is not defined. You're defining a attribute within a class scope, not an instance scope. Classes don't have a 'self', but instances do.

class Test(object):

    def __init__(self): 
        self.some_dictionary = {
            'key_one': self.method_one(1, 1),
            'key_two': self.method_two(),
            'key_three': self.method_three()
        }

This is the proper way to set an instance attribute, within a method. Notice that self is defined as the first (and only) argument to your method, in which the instance object is automatically passed as the first arg. Python programmers typically name this arg 'self', but you can name it whatever you like as long as you change references to it. E.g.,

class Test(object):

    class_attr = 'I am a class attribute.'

    def __init__(instance):
        instance.attr = 'I am an instance attribute.'
        print(Test.class_attr)
        print(self.class_attr)

'class_attr' is an attribute set within the class scope, and is accessible to the instance through the class (Test.class_attr) or through the instance itself (self.class_attr). This attribute is inherited by the instance from the class definition, and can be accessed even without an instance (through Test.class_attr).

    def method_one(self, x, y):
        add = x + y
        print "Add"
        return self.__init__.some_dictionary.get('key_two')

This fails because you don't have access to variables in the scope of __init__, and you're referencing some_dictionary incorrectly. Try:

    def dir_method(self):
        print(dir(self.__init__))

to see what you have access to through self.__init__.

/r/Python Thread