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

The Archive Base Latest Questions

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

I am trying to define a variable in a class that then can be

  • 0

I am trying to define a variable in a class that then can be accessed/changed from functions within that class.

For example:

class MyFunctions():
    def __init__( self):
        self.listOfItems = []

    def displayList( self):
        """Prints all items in listOfItems)"""
        for item in self.listOfItems:
            print item

    def addToList(self):
        """Updates all mlb scores, and places results in a variable."""
        self.listOfItems.append("test")

f = MyFunctions()
f.addToList
f.displayList

This should output all of the items in the list for me, but instead it displays nothing. I am assuming this is occuring because I did not setup the scope of the variables correctly. I want to be able to access and change listOfItems from within all of the functions in MyFuctions.

I have been trying to figure this out for a few hours now, so any help would be greatly appreciated.

  • 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-16T23:11:35+00:00Added an answer on May 16, 2026 at 11:11 pm

    f.addToList and f.displayList do not invoke the methods addToList and displayList respectively. They simply evaluate to the method (bound to the object f in this case) themselves. Add parentheses to invoke the methods as in the corrected version of the program:

    class MyFunctions():
        def __init__( self):
            self.listOfItems = []
    
        def displayList( self):
            """Prints all items in listOfItems)"""
            for item in self.listOfItems:
                print item
    
        def addToList(self):
            """Updates all mlb scores, and places results in a variable."""
            self.listOfItems.append("test")
    
    f = MyFunctions()
    f.addToList()
    f.displayList()
    

    This is in contrast to Ruby which does not require parentheses for method invocation (except to eliminate ambiguity in certain cases).

    It is instructive to add the following to the end of your program:

    print type(f.addToList)
    

    This will output something like the following:

    <type 'instancemethod'>
    

    demonstrating that this is a method reference and not a method invocation.

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

Sidebar

Related Questions

I am trying to define an abstract class that has operators to compare two
JavaScript has lexical scoping which means that non-local variables accessed from within a function
I'm trying to define a method in my vb.net interface that will only accept
I'm trying to define a service endpoint in my web.config file so that I
I'm trying to define a new array through a where query but I can
I am trying to store a pointer as a member variable, so that during
I'm trying to create a class that receives a URL to an XML file.
Still trying to get back into C++ and fumbling on differences from Java. Can
I am trying to create an applet that draws a christmas tree, then using
Within my MVC3 app I am trying to create a general class (named DdlGet

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.