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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:59:39+00:00 2026-05-15T13:59:39+00:00

Hello I need an uninstantiated class attribute and I am doing this: >>> class

  • 0

Hello I need an uninstantiated class attribute and I am doing this:

>>> class X:
...     def __init__(self, y=None):
...             self.y = list()

Is this ok? If no, is there another way of doing it. I can’t instantiate this attribute in __init__ cause I would be appending to this later.

  • 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-15T13:59:40+00:00Added an answer on May 15, 2026 at 1:59 pm

    Define the y var on the class-level attribute. You will need to initialize it to something, even it’s an empty list (as you were doing before).

    >>> class X:
    ...     y = [] 
    ...     def __init__(self):
    ...         pass
    

    Update based on your comments:

    You mentioned that you were mixed on the terminology (I’m assuming between class and instance variables), so here’s what’ll happen if you use this class (and is probably not what you want).

    >>> class X:
    ...     y = [] # Class level attribute
    ...     def __init__(self):
    ...             pass
    ... 
    >>> x = X()
    >>> x.y.append(1)
    >>> x.y
    [1]
    >>> x.y.append(2)
    >>> z = X()
    >>> z.y.append(3)
    >>> z.y
    [1, 2, 3]
    >>> X.y.append(4)
    >>> [1, 2, 3, 4]
    

    Notice that when you add a variable to the class it sticks around between constructions. In the previous code we instantiated the variable x and the variable z as an instance of X. While we added to the y variable in the z instance, we still appended to the class variable y. Note on the very last line (X.y.append(4)) I append an item using a reference to the class X.

    What you probably want is based off of your original post:

    >>> class X:
    ...     def __init__(self, y=None):
    ...             self.y = y or list() # Instance level attribute.  Default to empty list if y is not passed in.
    ... 
    >>> x = X()
    >>> x.y.append(1)
    >>> x.y
    [1]
    >>> z = X()
    >>> z.y.append(2)
    >>> z.y
    [2]
    >>> 
    >>> s = X()
    >>> s.y
    []
    >>> t = X(y=[10])
    >>> t.y
    [10]
    

    Notice how a new list is created with each instance. When we create a new instance z and try to append to the y variable we only get the value that was appended, rather than keeping all of the existing additions to the list. In the last instantiation (t) example the constructor passes in the y parameter in it’s construction, and thus has the list [10] as it’s y instance variable.

    Hopefully that helps. Feel free to ask any more uncertainties as comments. Also, I would suggest reading up on the Python class documentation here.

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

Sidebar

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.