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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:35:11+00:00 2026-05-23T21:35:11+00:00

I wish to make an instance of built-in class object , and use it

  • 0

I wish to make an instance of built-in class object, and use it as container for some variables (like struct in C++):

Python 3.2 (r32:88445, Mar 25 2011, 19:56:22) 
>>> a=object()
>>> a.f=2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'f'

Is there a way to accomplish this easier than doing:

>>> class struct():
...     '''Container'''
... 
>>> a=struct()
>>> a.f=2
>>> a.f
2
>>> 

UPDATE:

  • i need a container to hold some variables, but i don’t want to use
    dict – to be able to write a.f = 2 instead of a['f'] = 2

  • using a derived class saves you from typing the quotes

  • also, sometimes autocomplete works

  • 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-23T21:35:12+00:00Added an answer on May 23, 2026 at 9:35 pm

    I recognize the sentiment against dicts, and I therefore often use either NamedTuples or classes. Nice short hand is provided by Bunch in the Python Cookbook, allowing you to do declaration and assignment in one:

    point = Bunch(x=x, y=y, squared=y*y)
    point.x
    

    The printed Cookbook (2ed) has an extensive discussion on this.

    IMHO, the reason why object has no slots and no dict is readability: If you use an (anonymous) object to store coordinates first and then another object to store client data, you may get confused. Two class definitions makes it clear which one is which. Bunches don’t.

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

Sidebar

Related Questions

I have some REST web services implemented in WCF. I wish to make these
I wish to implement a 2d bit map class in Python. The class would
First some background. I'm parsing a simple file format, and wish to re-use the
I wish to make an object fade out and scale down 80% at the
I wish to make something looking like this: http://img291.imageshack.us/img291/5571/bartablestyling.png (Right now i only have
I wish to make sure that my data has a constraint the following check
I have a large system that I have coded and I wish to make
I am using jQuery, and wish to make certain links open in a new
I'm working on my high-school matriculation asp.net project, and I wish to make a
In C# I sometimes wish I could make special methods for certain instantiations of

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.