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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:32:10+00:00 2026-05-28T17:32:10+00:00

well I’m new to SO, OOP and python too, so please be gentle ;)

  • 0

well I’m new to SO, OOP and python too, so please be gentle 😉

I’ve looked for threads and explainations related to this scoping issue elsewhere and haven’t found any. I would be grateful for any assistance.

Sample code:

class Zeus(object):
    def __init__(self):
        self.name='zeus'
        self.maze=Maze()
        self.maze.get_zeus_name_1()
        self.maze.get_zeus_name_2(self)
        self.get_name_1()
        self.get_name_2(self)

    def get_name_1(self):
        try:
            print zeus.name
        except:
            print "impossible!?"

    def get_name_2(self,scope):
        print scope.name

class Maze(object):
    def get_zeus_name_1(self):
        try:
            print zeus.name
        except:
            print "can't be done!?"

    def get_zeus_name_2(self,scope):
        print scope.name

zeus=Zeus()
print 'now external calls:'
zeus.maze.get_zeus_name_1()
zeus.maze.get_zeus_name_2(zeus)
zeus.get_name_1()
zeus.get_name_2(zeus)

Output:

can't be done!?
zeus
impossible!?
zeus
now external calls:
zeus
zeus
zeus
zeus

During instantiation of zeus, if the __init__ method creates an instance of another class, maze, this new instance is not able to access its creator object, zeus (unless self is passed to it).
(Additionally, if the __init__ method calls a method within its own class, get_name_1, that method cannot access its objects attributes either (unless self is passed to it).)

However, AFTER the objects are both instantiated, the second object, maze can now recognise and access its creator object, zeus.

This behaviour has caused me some confusion and difficulties, as I was working on some code where everything was initialised and run from the __init__ sequence – now I suppose that it is better to avoid that..

My questions:

  1. Why is this the case?
  2. Are problems that can arise through this best avoided by leaving instantiation, out of __init__ calls?
  3. Are there further design implications?
  4. Passing self to a new instance, seems as though it could create problems, due to self-referencing, should this also be avoided?
  5. Other interesting implications/ Am I missing something?

Thanks for your help.

  • 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-28T17:32:11+00:00Added an answer on May 28, 2026 at 5:32 pm

    get_zeus_name_1() is trying to access the global variable zeus, which has not yet been initialised at the moment when get_zeus_name_1() is called.

    get_zeus_name_2() takes an argument (scope) and accesses that, which works. It doesn’t try to access the global variable zeus.

    Same story with get_name_1() and get_name_2().

    I think the key point is to understand how python executes this line:

    zeus=Zeus()
    

    This line says to python: execute the method Zeus() (which is another name for the __init__(self) method in the Zeus class), and then assign the object returned by that method to the global variable zeus.

    Python first executes the method Zeus() and then, after the init method has finished executing and returned an object-instance of the Zeus class, python assigns that object to the global variable zeus. So the global variable zeus has not been defined until after the init method finishes, so get_name_1() and get_zeus_name_1() cannot access it.

    get_name_2() and get_zeus_name_2() access the same object-instance of the Zeus class as get_name_1() and get_zeus_name_1() try to access, but they access it via a parameter that is passed to them, not via the global variable, so they don’t run into the problem.

    Here is a simpler example that demonstrates exactly the same problem:

    >>> class Foo:
    ...     def __init__(self):
    ...         self.name = 'foobar'
    ...         print self.name
    ...         print foo.name
    ... 
    >>> foo = Foo()
    foobar
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 5, in __init__
    NameError: global name 'foo' is not defined
    >>> 
    

    The print self.name line works just fine (equivalent to get_name_2() and get_zeus_name_2()), but the print foo.name line crashes (equivalent to get_name_2() and get_zeus_name_2()).

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

Sidebar

Related Questions

Well im new to javascript but why does this not work, all i want
Well, this is my first post here and really enjoying the site. I have
Well this is incredibly frustrating. After being nagged by Rails that I need to
well i have this messages table with sample values like these: msg_id recipient_id read
Well this is embarrassing ... I'm starting to play with the Eclipse Memory Analyzer
Well the issue I'm facing is I wish to update a mysql record whenever
Well, I have this bit of code that is slowing down the program hugely
Well I am new to Java web services and I need to develop a
Well, this must be a silly one. Here below is a can-not-be-simpler code in
Well I have a view of this type of hierarchy Main View (having some

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.