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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:24:56+00:00 2026-06-11T21:24:56+00:00

I’m trying create an intermediate object to work with elsewhere that I can pass

  • 0

I’m trying create an intermediate object to work with elsewhere that I can pass in to an sqlalchemy model for creation:

start with:

class IntermediateObj(object):

    def __init__(self, raw):
        self.raw = raw
        self.sections = []
        self.fields = []
        self.elements = []
        super(IntermediateObj, self).__init__()

    def recurse(self, items):
        for k,v in items.iteritems():
            if isinstance(v, list):
                getattr(self, k).append(v)
                [self.recurse(i) for i in v]
            else:
                setattr(self, k, v)

pass to:

class MyClass(IntermediateObj, Base):

    def __init__:(self, attribute=None):
         self.attribute = attribute 
         super(MyClass, self).__init__

e.g.

ii = IntermediateObj(raw={'large':'nested_dictionary', sections=[{}, {}, {}]})
ii.recurse(ii.raw) #any help smoothing this bit over appreciated as well, but another question...
tada = MyClass(ii)

tada.sections
---> [] /// this should not be, it should correspond to ii.sections

Sections is empty where it should not be, so I don’t quite grasp inheritance here yet. This has to be a common question, but I have not found anything I could understand at this point and am just flailing around at various tactics. Any input appreciated on doing python class inheritance correctly.

  • 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-11T21:24:57+00:00Added an answer on June 11, 2026 at 9:24 pm
    class IntermediateObj(object):
    
        def __init__(self, raw):
            self.raw = raw
            self.sections = []
            self.fields = []
            self.elements = []
    
        def recurse(self, items):
            # this works ok, but passing the same arguments to the recurse function
            # which were passed to the constructor as well ,and then stored as a class
            # attribute, why, seems like self.raw is not even needed?
            for k,v in items.iteritems():
                if isinstance(v, list):
                    getattr(self, k).append(v)
                    [self.recurse(i) for i in v]
                else:
                    setattr(self, k, v)
    
    
    class MyClass(IntermediateObj):
    
        def __init__(self, attribute=None):  
            self.attribute = attribute
            super(MyClass, self).__init__(attribute)
    
    
    ii = IntermediateObj({'large': 'nested_dictionary',
                          'sections': [{}, {}, {}]
                        })
    ii.recurse(ii.raw)
    print ii.raw
    print ii.sections
    
    # passing an instance of IntermediateObj is not the same as passing a dict (logically)
    # yet the constructor of MyClass just forwards that instance object to the
    # baseclasses constructor, while you initially passed a dict to the IntermediateObj
    # constructor. 
    tada = MyClass(ii)
    # MyClass inherits the recurse method, but it won't magically be executed unless
    # you call it, so just instantiating MyClass won't copy those values recursively
    tada.recurse(ii.raw)
    # now tada, it copied everything, and notice, I called recurse with ii.raw, which is the
    # source dictionary, but I'm not even sure if you wanted it that way, it's not clear
    # define your question more precisely
    print tada.sections
    

    I see you have accepted this as the best answer, and I felt bad about it cause it’s not really answered, so I made an update, as we described in the comments, now when an instance of MyClass receives an instance of IntermediateObj, it will call the constructor of the base class with the raw parameter of the passed object. Also recurse is called in IntermediateObj’s constructor, so it will copy the values at instantiation time:

    class IntermediateObj(object):
    
        def __init__(self, raw):
            self.raw = raw
            self.sections = []
            self.fields = []
            self.elements = []
            self.recurse(raw)
    
        def recurse(self, items):
            for k,v in items.iteritems():
                if isinstance(v, list):
                    getattr(self, k).append(v)
                    [self.recurse(i) for i in v]
                else:
                    setattr(self, k, v)
    
    
    class MyClass(IntermediateObj):
    
        def __init__(self, intermediate_instance):  
            super(MyClass, self).__init__(intermediate_instance.raw)
    
    
    ii = IntermediateObj({'large': 'nested_dictionary',
                          'sections': [{}, {}, {}]
                        })
    print ii.raw
    print ii.sections
    tada = MyClass(ii)
    print tada.sections
    print tada.raw
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.