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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:34:36+00:00 2026-06-12T05:34:36+00:00

class Foo(object): def __init__(self): self.list_one = [1, 2, 3] class Bar(object): def init(self): self.list_two

  • 0
class Foo(object):
    def __init__(self):
        self.list_one = [1, 2, 3]


class Bar(object):
    def init(self):
        self.list_two = Foo.list_one + [4, 5, 6] # I know this won't work unless I 
                                                 # make list_one a class level variable

So, the question is – is there any way to merge those lists without instantiation of Foo or making list_one a class level variable?

Maybe there is a way of doing so using inheritance and super() but I can’t understand how do I access variables inside methods with it. I also would be very grateful for an answer how to do it without using inheritance.

  • 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-12T05:34:38+00:00Added an answer on June 12, 2026 at 5:34 am

    It’s impossible to do the exact thing you describe. self is a reference to an instance of Foo, and doesn’t exist until the class is instantiated; and list_one is an attribute of self, assigned to self after it has already been created. So list_one doesn’t exist at all until after the class has been instantiated.

    To do what you’re asking using inheritance is fairly simple, however:

    class Foo(object):
        def __init__(self):
            self.list_one = [1, 2, 3]
    
    class Bar(Foo):
        def init(self):
            super(Bar, self).__init__()
            self.list_two = self.list_one + [4, 5, 6]
            # if you want to discard list_one, you can, but that seems wrong somehow
            # del self.list_one
    

    More generally, it’s not really meaningful to refer to “variables inside methods” except during the execution of the method or function, or when referring to what’s called the closure created by the function. Every time a function is called, a new namespace is created, and local variable names are created anew inside that namespace.

    It is possible to refer to them once the function has been called, though. For example:

    >>> def foo(x):
    ...     a = x
    ...     def bar():
    ...         print a
    ...     return bar
    ... 
    >>> bar1 = foo(5)
    >>> bar2 = foo(6)
    >>> bar1()
    5
    >>> bar2()
    6
    

    Observe that the two versions of bar refer to different values of a, each of which were created when foo(5) and foo(6) were called.

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

Sidebar

Related Questions

I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
Why doesn't this work as one may have naively expected? class Foo(object): def __init__(self):
Is there a difference between class Foo(object): bar = 1 def __init__(self): ... etc.
I want to do something like this: class Foo(object): def __init__(self, name): self._name =
import cPickle class Foo(object): def __init__(self): self._data = {'bar': 'baz'} def __getattr__(self, name): assert
For example, I need for class calling returns string. class Foo(object): def __init__(self): self.bar
If i had a class like this: class foo(object): def __init__(self, x, y, z):
When inheriting from two objects like these class Foo(object): def __init__(self,a): self.a=a class Bar(object):
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name

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.