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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:38:55+00:00 2026-06-10T21:38:55+00:00

Python (2.7) newbie here. What I want to do is have a reference to

  • 0

Python (2.7) newbie here. What I want to do is have a reference to an instance of a class return a given property as the ‘default’ without having to specify that property. I want to do this because the vast majority of the time I refer to an instance, it is to access this one property.

For example, let’s say I have a couple of classes to describes a book:

class book:
    def __init__(self):
        self.title = ''
        self.author = ''
        self.page = {}

class page:
    def __init__(self):
        self.text = ''
        self.length = 0

I create an instance and fill it with the contents of a book like so:

...    
war_and_peace = book()
page_count = 0
for page in pages:  #Let's say pages is a list of strings each of a page of the book
    pagenum += 1
    war_and_peace.page[pagenum] = page

Now in the remainder of the program, 99.9% of the time I reference the text of the pages. So to make my life a little easier, I would like to be able to reference a page using:

if 'Anna' in war_and_peace[15]:
    ...

instead of

if 'Anna' in war_and_peace.page[15].text:
    ...

How might I go about this? (Sorry if this has been answered; I am probably using the wrong search terms!)

Thanks!

  • 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-10T21:38:57+00:00Added an answer on June 10, 2026 at 9:38 pm

    A __getitem__ override on book will give you this:

    def __getitem__(self, item):
        return self.page[item].text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

newbie python programmer here. I have written a script and want to modify the
I am a Python newbie. I have this small problem. I want to print
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Here's a bit of a newbie Python question about instance variables. Consider the following
Python newbie here: I'm writing a market simulation in Python using Pysage, and want
I am newbie in Python and I need help: I have a file with
As a newbie to Python, and mainly having a background of writing scripts for
I'm newbie for python, I'm having task so I need to scan wifi and
I'm newbie in Python. I have this simple code a = 0 b =
Python newbie here and wanted to know if there was a way to combine

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.