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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:02:50+00:00 2026-06-16T00:02:50+00:00

I have this scenario, where I need to ask a nested class to append

  • 0

I have this scenario, where I need to ask a nested class to append items to a list in the outer class. Heres pseudocode thats similar to what Im trying to do. How would I go about getting it to work?

class Outer(object):
  outerlist = []
  class Inner(object):
    def __call__(self, arg1):
      outerlist.append(arg1)

if __name__ == "__main__":
  f = Outer()
  f.Inner("apple")
  f.Inner("orange")

  print f.outerlist()

This is what I hope to see – apple, orange

Details:
OS X, Python 2.7

  • 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-16T00:02:51+00:00Added an answer on June 16, 2026 at 12:02 am

    Now that I understand your design, you’re going about things all wrong.

    First, Outer is a class; it doesn’t get __call__ed. Your line 17 is just going to construct an empty Outer object and do nothing with it. If you want to “call” the Outer object, you can define an __init__ method—or, as Sheena suggests, define a __new__ and intercept the initialization, since you don’t actually need the initialized object anyway.

    Honestly, I don’t think someone who doesn’t understand how __call__ works yet should be trying to build something tricky like this yet. But if you insist, read on.

    It’s a very odd, and probably bad, design to collect this kind of stuff in a class instead of an instance. Keep in mind that class variables are effectively globals, with all that entails. If you try to use Outer reentrantly, or from multiple threads/event handlers/greenlets/whatever, the uses will end up stomping all over each other. Even if you think that isn’t possibly going to be a problem now, it likely will at some point in the future.

    You could create an Outer instance, and use its members as decorators. For example:

    from outer_library import Outer
    
    outer = Outer()
    
    @outer.get("/")
    …
    

    But I’m not sure that’s much better. The entire design here seems to involve performing actions at the module level, even though it looks like you’re just defining normal functions and calling a function at the end. The fact that you’ve managed to confuse yourself should be evidence of how confusing a design this is.

    But if you do want to do that, what you probably want to do is define classes inside the Outer.__init__ method, and assign them to instance members. Classes are first-class values, and can be assigned to variables just like any other values. Then, use the __init__ (or __new__) methods of those classes to do the work you wanted, making the classes simulate functions.

    This may seem confusing or misleading. But remember that the whole point of what you’re trying to do is to use a class in a way that it looks like a method, so that kind of confusion is inherent in the problem. But if you prefer, you can write decorators as functions (in this case, as normal instance methods of Outer); it just makes a different part of the problem harder instead of this part.

    A more normal way to design something like this would be to make Outer a perfectly normal class that people can either subclass or create instances of, and provide an explicit non-fancy way to attach handler methods or functions to URLs. Then, once that’s working, design a way to simplify that handler registration with a decorator.

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

Sidebar

Related Questions

I have this scenario. I need to show 3 list boxes on a ASP.NET
In my app, I have this scenario where I need to post an object
I have a scenario like this: I need to pass @id to the stored
I have this scenario well, i'll let the model explain. public class ScheduleMonthlyPerDayModel {
So I have this scenario <div id=editor contenteditable=true> <div id=list>Hello</div> </div> I want to
In this scenario, I have 2 or more models: class Store(models.Model): name = models.CharField(max_length
I have this scenario: I need to know how to do in apache the
This question is about a whole class of similar problems, but I'll ask it
I have this scenario where I need data integrity in the physical database. For
I have this scenario. I want group on Country and Category. A store can

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.