PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] why does the code print the "test message" although I wrote just the code for printing the attribute "a"

 August 24, 2022     module, printing, python, python-3.x     No comments   

Issue

I don't know why it prints both "test message" and "ABC" instead of just "ABC". I think just wrote the code for printing the attribute "a" but it prints something more!

I have two modules: "first.py" & "second.py"

first.py is:

import second
print(second.a)

second.py is:

a="ABC"
print("test message")

OUTPUT is:

test message
ABC

Solution

import second will run all code in second.py. print("test message") is also executed. If you want to prevent this use below

a = 'ABC'
if __name__ == "__main__":
    print("test message")

if __name__ == "__main__": will only run when run from that file.



Answered By - Desty
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,204,395

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing