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

  • Home
  • SEARCH
  • 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 8401293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:46:07+00:00 2026-06-09T21:46:07+00:00

I’m learning python via book and internet and I’m stuck on a class issue.

  • 0

I’m learning python via book and internet and I’m stuck on a class issue.

2 questions:

  1. How do I create an instance of one class in another (separate) class?
  2. How do I pass variables between the class and the nested(?) class?

When I try to create an instance of a class within another (separate) class, i’m able to do so within a method. Here’s the code:

import os

class FOO():
    def __init__(self):
        self.values = [2, 4, 6, 8]

    def do_something(self, x, y):
        os.system("clear")
        z = self.values[x] * self.values[y]
        print "%r * %r = %r" % (self.values[x], self.values[y], z)


class BAR():
    def __init__(self):
        pass

    def something_else(self, a, b):
        foo1 = FOO()
        foo1.do_something(a, b)

bar = BAR()
bar.something_else(1, 2)

Will this, however, allow me to access the FOO class and it’s information in other BAR methods? If so, how?

I tried the following and got an error:

import os

class FOO():
    def __init__(self):
        self.values = [2, 4, 6, 8]

    def do_something(self, x, y):
        os.system("clear")
        z = self.values[x] * self.values[y]
        print "%r * %r = %r" % (self.values[x], self.values[y], z)


class BAR():
    def __init__(self):
        foo1 = FOO()

    def something_else(self, a, b):
        foo1.do_something(a, b)

bar = BAR()
bar.something_else(1, 2)

Here is the error:

Traceback (most recent call last):
File "cwic.py", line 22, in <module>
bar.something_else(1, 2)
File "cwic.py", line 19, in something_else
foo1.do_something(a, b)
NameError: global name 'foo1' is not defined

I get the same error when I used:

def __init__(self):
    self.foo1 = FOO()

Also, how should I pass the ‘values’ from one class to the other?

Please let me know if my general approach and/or syntax are wrong.

  • 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-06-09T21:46:08+00:00Added an answer on June 9, 2026 at 9:46 pm

    All attributes of an instance or class are accessed via self which is passed as the first argument to all methods. That’s why you’ve correctly got the method signature something_else(self, a, b) as opposed to just something_else(a, b) as you might with other languages. So you’re looking for:

        class BAR():
            def __init__(self):
                self.foo1 = FOO()
    
        def something_else(self, a, b):
            self.foo1.do_something(a, b)
    

    Note that self isn’t a keyword, it’s just a paramater name like a and b, so you might for example be tempted to use this instead if you’re from a Java background… Don’t though! there is a very strong convention to use self for this purpose and you will make a lot of people (including yourself eventually) very angry if you use anything else!

    With regards to passing values I’m not sure what you mean, you can access them through instances such as foo1.an_attribute or pass them as arguments to functions as in function(arg) but you seem to have used both techniques in your example so I’m not sure what else you’re after…

    Edit

    In response to @dwstein’s comment, I’m not sure how it’s done in VB but here’s a how you pass arguments when creating an instance of a class.

    If we look at your class:

    class BAR():
        def __init__(self):
            self.foo1 = FOO()
    

    We can change that to accept an argument baz by changing second line to def __init__(self, baz): and then if we want we can make baz an attribute of our new instance of BAR by setting self.baz = baz. Hope that helps.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an XML file, the creators of it stuck in a bunch social
I know there's a lot of other questions out there that deal with this
I'm making a simple page using Google Maps API 3. My first. One marker

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.