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
  • 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-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

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT p.Id, p.name, MAX(CASE RowNum WHEN 1 THEN FriendName ELSE… May 16, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer Any modern http lib support Gzip compression, it's part of… May 16, 2026 at 5:59 pm
  • Editorial Team
    Editorial Team added an answer Avoid the problem by using rvm. You can create and… May 16, 2026 at 5:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For example, suppose I have a class: class Foo { public: std::string& Name() {
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
I have a class that contains a member object. I would like to call
Suppose you have the following situation #include <iostream> class Animal { public: virtual void
Suppose one wants to build a novel generic class, Novel[A] . This class will
Suppose I have an XML-serializable class called Song : [Serializable] class Song { public
Suppose (for the sake of argument) that I have a view class which contains
I currently have the following code: events.detect do |event| #detect does the block until
Can jquery-ajax reload a div WITH DYNAMIC CONTENTS. For example if i have a
This is not exactly a question, it's rather a report on how I solved

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.