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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:46:47+00:00 2026-06-11T01:46:47+00:00

The below code is not acting as expected for me. class stateClass: state =

  • 0

The below code is not acting as expected for me.

class stateClass:
  state = 0

states = []
states.append(stateClass)
states.append(stateClass)

def populateStates(states):
  for s in states:
    if s.state == 0
      print 'populating'
      s.state = 1

populateStates(states)

the output is

states array length: 2
populating

this is failing the second time

for s in states:
  if s.state == 0

if conditional is failing the second time although it is a different index in the array and thus the s.state should have been initialized to 0. So I think the loop is not iterating properly.

Anyone know whats wrong?

  • 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-11T01:46:49+00:00Added an answer on June 11, 2026 at 1:46 am

    You don’t need the ;‘s – this isn’t C and co. etc…

    class stateClass: 
        state = 0 
    

    Here you’re creating a class level attribute – ie, state is shared by all instances of stateClass.

    You’re appending to your list stateClass itself – ie, the definition of the class, not an actual instance of a class…

    As soon as you change state in one of them, all instances of stateClass now have that changed value.

    You most likely want to be creating instances:

    class State(object):
        def __init__(self, number):
            self.state = number
    
    Hawaii = State(50)  
    Kentucky = State(23) # (making state numbers up here...) 
    
    my_states = []
    my_states.append(Hawaii)
    my_states.append(Kentucky)
    

    To then show state, do something like:

    print Hawaii.state
    

    If you wanted a property so it avoids explicit setting from outside the class then:

    class State(object):
        def __init__(self, number):
            self._state = number
        @property
        def state(self):
            return self._state
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below code can not run def map = [name:Test :: ( %2f %25 \$
Can someone tell me why the below code is not working? public partial class
Below code not work, but it's work fine for jsf1.2. Now the framework is
The code below does not work but meant to illustrate what I want to
When i tried using the below code...the td tag is returned and not the
I am getting an compilation error not a statement for the below code. Not
I am using the below code, but I am not getting that why it
I am using the below code to initialise a jQuery FullCalendar. However, not all
I am having a problem with the below code. I am not sure why
The code below produces an error saying that my input string was not in

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.