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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:02:44+00:00 2026-05-20T10:02:44+00:00

I am getting my head around Python for the first time and I am

  • 0

I am getting my head around Python for the first time and I am stuck here:

class A:
    def __init__(self):
        a = foo("baa")

class B(A):
    b = foo("boo")

def foo(string):
    return string

At this point I load the above file (named classes) and this happens:

$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "classes.py", line 5, in <module>
    class B(A):
  File "classes.py", line 6, in B
    b = foo("boo")
NameError: name 'foo' is not defined

Notice how the error is in class B, where foo is called directly and not from __init__. Notice also how I have not yet instantiated the class B.

First question:

  • Why is it returning an error? I am not instantiating a class.

Moving on. The ‘problem’ is solved by moving the definition of foo() a few lines above:

def foo(string):
    return string

class A:
    def __init__(self):
        a = foo("baa")

class B(A):
    b = foo("boo")

Now I can do

>>> x = B()
>>> x.b
'boo'

but I can’t do

>>> y = A()
>>> y.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute 'a'

Further questions:

  • What is that I have not understood about __init__?

I don’t think this is the same as a forward declaration, hence I hope this question is not a duplicate.

On a side note, I am aiming towards implementing a DSL, but that’s mostly an excuse to get myself to learn python.

  • 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-20T10:02:45+00:00Added an answer on May 20, 2026 at 10:02 am

    First, in class B, the function foo() is called before being declared. A does not have this problem because foo() is only called when the class is instantiated–after the function foo is defined.

    For your second question, y.a will not work because you did not say self.a = foo('stirng'). a = foo('stirng') only creates the variable a in the scope of __ init __.

    An ideal __ init __ function assigns variables to the instance self.

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

Sidebar

Related Questions

For some reason I am having trouble getting my head around __init__ and __new__
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
I need help getting my head around the difference between my current OOP notion
When designing a new system or getting your head around someone else's code, what
Just getting my head around Linq and having lots of fun! Can any one
I am still getting my head around MySQL INDEXES... A quick question... I have
Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
I'm writing a Python wrapper for a C++ library, and I'm getting a really
Getting started with jquery and having trouble getting hello world type example going for
Without getting a degree in information retrieval, I'd like to know if there exists

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.