Variable from a Class to a Dictionaries keys and values in to Function in Python

Viewed 37

I am not a programmer and very new to programming. Trying to learn and understand python its way of working with classes , functions and variables.

I created this code below.

class Test:
    number = 1
    field = ['Address', 'Name', 'Age']

    def book(self):
        myDict = self.book
        myDict = {number : field}
        # Need my Output to be myDict = {1 : ['Address', 'Name', 'Age']}
        return myDict

It doesn't show any error while compiling but throws while calling the instance of that object

>>> te = Test()
>>> te.book()

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
    te.book()
    myDict = {number : field}
NameError: name 'number' is not defined

What I have in my mind is that, the variable declared outside the function will be global and will be called in to the function if that variable is not available inside the function, can somebody please clarify me where I am going wrong understanding this concept.

2 Answers
Related