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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:43:10+00:00 2026-06-11T10:43:10+00:00

I have a class with a number of attributes. Could someone please clarify the

  • 0

I have a class with a number of attributes. Could someone please clarify the differences between setting attributes like a,b,c below and x,y,z. I understand that if you arguments to come in they obviously must be in the init, however, just for setting a number default variables, which is preferred and what are the pros and cons.

class Foo(object):
    a = 'Hello'
    b = 1
    c = False
    def __init__(self):
        self.x = 'World'
        self.y = 2
        self.z = True
  • 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-11T10:43:11+00:00Added an answer on June 11, 2026 at 10:43 am

    Variables a, b, and c are class variables. They’re evaluated and set once, when the class is first created. Variables x, y, and z are instance variables, which are evaluated and set whenever an object of that class is instantiated.

    In general, you use class variables if the same values are going to be used by every instance of the class, and only needs to be calculated once. You use instance variables for variables that are going to be different for each instance of that class.

    You can access class variables through the instance variable syntax self.a, but the object in question is being shared between all instances of the class. This doesn’t affect much when using immutable data types such as integers or strings, but when using mutable data types such as lists, appending to self.a would cause all instances to see the newly-appended value.

    Some examples from IDLE are probably helpful in understanding this:

    >>> class Foo(object):
        a = 'Hello'
        b = []
        def __init__(self):
            self.y = []
    
    >>> instance_1 = Foo()
    >>> instance_2 = Foo()
    >>> instance_1.a
    'Hello'
    >>> instance_2.a
    'Hello'
    >>> instance_1.a = 'Goodbye'
    >>> instance_1.a
    'Goodbye'
    >>> instance_2.a
    'Hello'
    >>> instance_1.b
    []
    >>> instance_2.b.append('12345')
    >>> instance_1.b
    ['12345']
    >>> instance_2.y
    []
    >>> instance_2.y.append('abcde')
    >>> instance_2.y
    ['abcde']
    >>> instance_1.y
    []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a class that contains a number of typedef 'd variables, like
I have a class that spawns an arbitrary number of worker object that compute
I'm testing a C++ class with a number of functions that all have basically
I have a number of classes that register with a notification class in my
I have a class which is called a number of times. When the application
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have simple class with width and height member fields which define number of
I have a simple custom NumberField: class NumberInput(forms.widgets.Input): input_type = 'number' class NumberField(forms.DecimalField): def
I have a Version class that contains a version_number string, and can obtain a
I have a number of objects, all from the same class(ColorNum) Each object has

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.