3

How to access variables from class constructor to the class functions?

Code:

class evol:
    k = 0
    m = 0

    def __init__(self, file1):
        InFile = open(file1, 'rb')
        InFile = csv.reader(InFile, delimiter='\t')
        for rec in InFile:
            self.k += int(rec[0])

    def method1():
        print k

I get the error, NameError: global name 'k' is not defined

3
  • You need def method(self): print self.k. You may be familiar with other programming languages that use the this keyword, its basically that. self is just the convention to pass the reference of itself. You could also do def method(self): print evol.k. In python class variables can be accessed like that. In either case, the method1 needs param self so that it knows its a bound method rather than a static or some other function Commented Oct 10, 2014 at 16:00
  • 1
    Thank you. When i access k in method1() using evol.k, it is printing initial value defined under class. However, using self.k, it print the modified value from constructor. Commented Oct 10, 2014 at 16:10
  • That is true. In Python a class variable will remain that value for the class and can be modified via the class. Since it doesn't have self as a reference as in self.k = 12 under __init__, it doesn't know who it's referring to. That's why it's always better to initialize variables under __init__ Commented Oct 10, 2014 at 18:59

2 Answers 2

2

You must use the full name of the class variable:

def method1(self):
    print evol.k

or, since you assign to self.k in __init__, you might want

def method1(self):
    print self.k

instead, depending on which k you mean.

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

2 Comments

Thank you. When i access k in method1() using evol.k, it is printing initial value defined under class. However, using self.k, it print the modified value from constructor
As an aside, it's better to put self.k = 0 in __init__ if you want to provide a default value in case another value is not supplied later. Reserve class variables for things that are independent of instances, or truly shared by all instances.
2

I don't have enough rep to add a comment, but are you sure you are indenting your code? Still, you can access to class variables using "self" keyword:

class evol:
    k = 0
    m = 0

    def __init__(self, file1):
        InFile = open(file1, 'rb')
        InFile = csv.reader(InFile, delimiter='\t')
        for rec in InFile:
            self.k += int(rec[0])

    def method1(self):
        print self.k

If you don't indent your code, method1() will be a standalone method outside the class, so it will not know anything about k

1 Comment

Thank you. I can not upvote this post as i have less reputation.

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.