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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:42:15+00:00 2026-05-26T21:42:15+00:00

I have two files that I am trying to work with. menu.py has the

  • 0

I have two files that I am trying to work with. menu.py has the menus that program.py imports.

menu.py:

import cmd,sys

user = 'no one'

class loginMenu(cmd.Cmd):
    def postloop(self):
        user = 'lilith'
        return user
    intro="""login menu"""
    def do_smtoggle(self,person):
        return True

    def do_quit(self,person):
        sys.exit()

class storeMenu(cmd.Cmd):
    intro="""store menu"""
    def do_whoami(self,person):
        print 'storemenu ' + user
    def do_quit(self,person):
        quit = True
        return quit

program.py:

from menu import *
import cmd,sys

lm = loginMenu()
sm = storeMenu()

while True:
    lm.cmdloop()
    print user
    sm.cmdloop()

I expect that when program.py gets to print user it will print lilith but instead it prints no one. Why is this? postloop() is defined in the docs as a method that executes when cmdloop() is about to return so I thought it would return the value of user as lilith and then lilith would be printed but it just prints no one.

edit: It has been recommended that I not use globals. I am reading this to find out why.

  • 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-26T21:42:15+00:00Added an answer on May 26, 2026 at 9:42 pm

    That’s because the assignment to user here:

    def postloop(self):
        user = 'lilith'
        return user
    

    is acting on a local instance of the var. To affect the global var user, you would need to add a:

    global user
    

    to the top of that function. Though I wouldn’t recommend it, as there are usually better ways to do things than to use globals.

    One note about globals and python. While you can’t assign a value to a global var without the global keyword, you can read global (and other non-local) scopes, without it. This allows you to make closures.

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

Sidebar

Related Questions

I wanted to write a program that test if two files are duplicates (have
So I have two files: File 1 has this method in it: import MyGlobals
What I'm trying to accomplish: I have two base files on my server that
I have two PHP files that I need to link. How can I link
I have two properties files that are not the same and I need to
I have two XML files that are generated by another application I have no
I have two text files that contain columnar data of the variety position -
Suppose that you have two huge files (several GB) that you want to concatenate
I have two large (~100 GB) text files that must be iterated through simultaneously.
I have this c# web app that relies heavily on two dll files that

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.