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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:03:22+00:00 2026-05-23T03:03:22+00:00

This is a weird behavior. Try this : rep_i=0 print "rep_i is" , rep_i

  • 0

This is a weird behavior.

Try this :

rep_i=0
print "rep_i is" , rep_i
def test():
  global rep_i #without Global this gives error but list , dict , and others don't
  if rep_i==0:
    print "Testing Integer %s" % rep_i
    rep_i=1
  return "Done"

rep_lst=[1,2,3]
 

def test2():
  if rep_lst[0]==1:
    print "Testing List %s" % rep_lst
  return "Done"


if __name__=="__main__":
  test()
  test2()

Why list do not need to declare global? are they automatically global?

I find it really weird, I use list most of the time and I don’t even use global at all to us them as global…

  • 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-23T03:03:23+00:00Added an answer on May 23, 2026 at 3:03 am

    It isn’t automatically global.

    However, there’s a difference between rep_i=1 and rep_lst[0]=1 – the former rebinds the name rep_i, so global is needed to prevent creation of a local slot of the same name. In the latter case, you’re just modifying an existing, global object, which is found by regular name lookup (changing a list entry is like calling a member function on the list, it’s not a name rebinding).

    To test it out, try assigning rep_lst=[] in test2 (i.e. set it to a fresh list). Unless you declare rep_lst global, the effects won’t be visible outside test2 because a local slot of the same name is created and shadows the global slot.

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

Sidebar

Related Questions

I've seen this weird behavior on several sites recently: I scroll down a page
This is the day of weird behavior. We have a Win32 project made with
I got this weird error which mentions I should install VSTS 2008 SP1, during
I got this weird error when I wanted to validate my in-memory xml Schema,
I`m really confused here about this weird behavior! The thing is, I have a
We're having this weird behavior regarding our logs. We're using log4net and here is
This is a weird behavior that I just can't understand: I access my own
I found weird behavior in PHP, it looks like reversed register globals. First try
What could possibly cause this weird python behaviour? Python 2.6.2 (r262:71600, May 31 2009,
This struck me as really weird behaviour and I spent a while checking for

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.