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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:46:20+00:00 2026-05-14T00:46:20+00:00

I have the following Python 2.6 program and YAML definition (using PyYAML ): import

  • 0

I have the following Python 2.6 program and YAML definition (using PyYAML):

import yaml

x = yaml.load(
    """
        product:
           name     : 'Product X'
           sku      : 123
           features :
             - size    :  '10x30cm'
               weight  :  '10kg'

         """
    )

print type(x)
print x

Which results in the following output:
<type 'dict'>
{'product': {'sku': 123, 'name': 'Product X', 'features': [{'weight': '10kg', 'size': '10x30cm'}]}}

It is possible to create an object with fields from x?

I would like to the following:

print x.features[0].size

I am aware that it is possible to create and instance from an existing class, but that is not what I want for this particular scenario.

Edit:

  • Updated the confusing part about a ‘strongly typed object’.
  • Changed access to features to a indexer as suggested Alex Martelli
  • 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-14T00:46:20+00:00Added an answer on May 14, 2026 at 12:46 am

    So you have a dictionary with string keys and values that can be numbers, nested dictionaries, lists, and you’d like to wrap that into an instance which lets you use attribute access in lieu of dict indexing, and “call with an index” in lieu of list indexing — not sure what “strongly typed” has to do with this, or why you think .features(0) is better than .features[0] (such a more natural way to index a list!), but, sure, it’s feasible. For example, a simple approach might be:

    def wrap(datum):
      # don't wrap strings
      if isinstance(datum, basestring):
        return datum
      # don't wrap numbers, either
      try: return datum + 0
      except TypeError: pass
      return Fourie(datum)
    
    class Fourie(object):
      def __init__(self, data):
        self._data = data
      def __getattr__(self, n):
        return wrap(self._data[n])
      def __call__(self, n):
        return wrap(self._data[n])
    

    So x = wrap(x['product']) should give you your wish (why you want to skip that level when your overall logic would obviously require x.product.features(0).size, I have no idea, but clearly that skipping’s better applied at the point of call rather than hard-coded in the wrapper class or the wrapper factory function I’ve just shown).

    Edit: as the OP says he does want features[0] rather than features(0), just change the last two lines to

      def __getitem__(self, n):
        return wrap(self._data[n])
    

    i.e., define __getitem__ (the magic method underlying indexing) instead of __call__ (the magic method underlying instance-call).

    The alternative to “an existing class” (here, Fourie) would be to create a new class on the fly based on introspecting the wrapped dict — feasible, too, but seriously dark-gray, if not actually black, magic, and without any real operational advantage that I can think of.

    If the OP can clarify exactly why he may be hankering after the meta-programming peaks of creating classes on the fly, what advantage he believes he might be getting that way, etc, I’ll show how to do it (and, probably, I’ll also show why the craved-for advantage will not in fact be there;-). But simplicity is an important quality in any programming endeavor, and using “deep dark magic” when plain, straightforward code like the above works just fine, is generally not the best of ideas!-)

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

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Then this will work for you... Prejoin a Cartesian against… May 14, 2026 at 6:58 pm
  • Editorial Team
    Editorial Team added an answer You could compute the difference in days between the start… May 14, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer Perhaps have a look into the DrawBitmap and BitBit functions. May 14, 2026 at 6:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.