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 975715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:37:53+00:00 2026-05-16T03:37:53+00:00

Suppose I have this code: class Example(object): def the_example(self): itsProblem = "problem" theExample =

  • 0

Suppose I have this code:

class Example(object):
    def the_example(self):
        itsProblem = "problem"

theExample = Example()
print(theExample.itsProblem)

When I try it, I get an error that says:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Example' object has no attribute 'itsProblem'

How do I access this attribute? I tried adding another method to return it:

    def return_itsProblem(self):
        return itsProblem

but the problem persists.

  • 1 1 Answer
  • 3 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-16T03:37:54+00:00Added an answer on May 16, 2026 at 3:37 am

    The answer, in a few words

    In your example, itsProblem is a local variable.

    Your must use self to set and get instance variables. You can set it in the __init__ method. Then your code would be:

    class Example(object):
        def __init__(self):
            self.itsProblem = "problem"
    
    
    theExample = Example()
    print(theExample.itsProblem)
    

    But if you want a true class variable, then use the class name directly:

    class Example(object):
        itsProblem = "problem"
    
    
    theExample = Example()
    print(theExample.itsProblem)
    print (Example.itsProblem)
    

    But be careful with this one, as theExample.itsProblem is automatically set to be equal to Example.itsProblem, but is not the same variable at all and can be changed independently.

    Some explanations

    In Python, variables can be created dynamically. Therefore, you can do the following:

    class Example(object):
        pass
    
    Example.itsProblem = "problem"
    
    e = Example()
    e.itsSecondProblem = "problem"
    
    print Example.itsProblem == e.itsSecondProblem 
    

    prints

    True

    Therefore, that’s exactly what you do with the previous examples.

    Indeed, in Python we use self as this, but it’s a bit more than that. self is the the first argument to any object method because the first argument is always the object reference. This is automatic, whether you call it self or not.

    Which means you can do:

    class Example(object):
        def __init__(self):
            self.itsProblem = "problem"
    
    
    theExample = Example()
    print(theExample.itsProblem)
    

    or:

    class Example(object):
        def __init__(my_super_self):
            my_super_self.itsProblem = "problem"
    
    
    theExample = Example()
    print(theExample.itsProblem)
    

    It’s exactly the same. The first argument of ANY object method is the current object, we only call it self as a convention. And you add just a variable to this object, the same way you would do it from outside.

    Now, about the class variables.

    When you do:

    class Example(object):
        itsProblem = "problem"
    
    
    theExample = Example()
    print(theExample.itsProblem)
    

    You’ll notice we first set a class variable, then we access an object (instance) variable. We never set this object variable but it works, how is that possible?

    Well, Python tries to get first the object variable, but if it can’t find it, will give you the class variable. Warning: the class variable is shared among instances, and the object variable is not.

    As a conclusion, never use class variables to set default values to object variables. Use __init__ for that.

    Eventually, you will learn that Python classes are instances and therefore objects themselves, which gives new insight to understanding the above. Come back and read this again later, once you realize that.

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

Sidebar

Related Questions

For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
Suppose I have this code: class A { }; class B: virtual public A
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Suppose I have this jQuery code. $(document).delegate('a[title]', 'click', function(event) { var txt = //
I have this code that suppose to place the marker by the latlng public
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have the code like this: <table> <tr> <th>name</td> <th>price</td> </tr> <tr> <td>a</td>

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.