5
  • Operating System: Windows, 64bit
  • Python Version: 3.7.11
  • IDE: Jupyter Notebook (with conda env)

I have below code:

class Vocabulary(object):
    
    PAD_token = 0
    
    def __init__(self):
        self.index2word = {PAD_token: "PAD"}

# create object
voc = Vocabulary()

I want to use PAD_token class variable inside __init__ method but I got below error:

NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8472/897996601.py in <module>
----> 1 voc = Vocabulary()

~\AppData\Local\Temp/ipykernel_8472/3780152240.py in __init__(self)
      4 
      5     def __init__(self):
----> 6         self.index2word = {PAD_token: "PAD", SOS_token: "SOS", EOS_token: "EOS"}

NameError: name 'PAD_token' is not defined

Question:

  • How can I use PAD_token class variable inside __init__ or other methods of the class?
3
  • 1
    self.PAD_token will work Commented Dec 20, 2021 at 13:25
  • Also defining variables this way makes it shared between all instances of that class, so watch out. (You don',t even have to initialize an instance to access it, it becomes static) Commented Dec 20, 2021 at 13:26
  • @Ahmed AEK This is exactly what I want. I want that all instances access the same thing. Commented Dec 20, 2021 at 13:28

4 Answers 4

4

There are two ways to access it

first: self.__class__.PAD_token

second: self.PAD_token

If you just need to access class variables, the first one is recommended

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

5 Comments

It will be all right. as long as you use it after the class is created.
The first one is recommended by who, and for what reason?
self.__class__.PAD_token will go directly to the class namespace to find. self.PAD_token will first go to self's namespace to find, if not found, it will go to class to find
Of course, the choice of this depends on the specific situation. Sometimes if the PAD_token of different instances is different, self.PAD_token is preferred instead of self.__class__PAD_token
@kaya3 I recommend first because If just need to access class variables
2

You are trying access variable that you assigned in your class. Here PAD_token can be called as class variable.

you can access it by class name Vocabulary.PAD_token or by self self.PAD_token.

In your case dictionary will place its value to the key i.e.

{0:"PAD"} #ignoring other keys

because you have assigned that to 0 in initialization.

Comments

0

write it with self keyword like self.PAD_token.

Comments

0

To access any class variable from within the object, you need to put self. in front of the variable name.

This means that to call PAD_token you need to use self.PAD_TOKEN

Your code should look like this.

class Vocabulary(object):
    
    PAD_token = 0
    
    def __init__(self):
        self.index2word = {self.PAD_token: "PAD"}

# create object
voc = Vocabulary()

If you do not add the self it will think you are trying to access a local variable named PAD_token, and there isn't a local variable that has been defined within that method with that name, that is why it is throwing that not defined error.

Comments

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.