Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:56:39+00:00 2026-05-30T14:56:39+00:00

class test: def __init__(self): test_dict = {‘1’: ‘one’, ‘2’: ‘two’} def test_function(self): print self.test_dict

  • 0
class test:
    def __init__(self):
        test_dict = {'1': 'one', '2': 'two'}
    def test_function(self):
        print self.test_dict

if __name__ == '__main__':
    t = test()
    print t.test_dict

Error:

AttributeError: test instance has no attribute 'test_dict'

Also, if i execute code: t.test_function() instead of print t.test_dict, error occurred too:

AttributeError: test instance has no attribute 'test_dict'

Why? i have defined test_dict in function __init__, so it should be initialized to each instance, but why does python tell me it cannot find the dict?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:56:41+00:00Added an answer on May 30, 2026 at 2:56 pm

    Think of classes/instances as dictionaries. Whenever you create instance and call any of its methods, these functions automatically receive instance as first argument (unless function is static or class method).

    So, if you want some variable to be stored in instance and later be accessed, put all variables into that first argument (by convention, it is called self).

    Class constructor is not an exception of the above rule. That’s why all answers point out a change in the constructor in test_dict assignment.

    Think of:

    self.test_dict = ...
    

    like

    self.__dict__["test_dict"] = ...
    

    Like will all variables in Python, you can not access it if variable was not assigned first. This is the case in your original class:

    _init_ has created a local (to method) variable, while test_function is trying to access instance variable in dictionary, which does not exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have this code class Test(object): def start_conn(self): pass def __init__(self): self.conn =
Why can't I perform an action like the following: class Test(object): def __init__(self): self
import time import threading class test(threading.Thread): def __init__ (self): threading.Thread.__init__(self) self.doSkip = False self.count
class test: def __init__(self, val): self.val = val self.val.lower() Why doesn't lower() operate on
I have two files, one of the test.py is import new.py class Test: def
I get this error object has no attribute 'im_func' with this class Test(object): def
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
How can it be that this test case import unittest class PropTest(unittest.TestCase): def test(self):
i have the following class test hash={} def printHash puts hash[1] puts hash[2] puts
I currently have a unittest.TestCase that looks like.. class test_appletrailer(unittest.TestCase): def setup(self): self.all_trailers =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.