简体   繁体   English

全局名称未定义python

[英]global name is not defined python

I get a NameError: global name 'valid_username' is not defined which confuses me. 我收到一个NameError:全局名称'valid_username'未定义使我感到困惑。 Why does that happen? 为什么会这样呢? I declared the valid_username function before actually calling it so what's the problem? 我在实际调用函数之前声明了valid_username函数,这是什么问题?

Here's the code full code: 这是完整的代码:

import webapp2
import cgi
import re

class WelcomeHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Welcome, ")


class MainPage(webapp2.RequestHandler):
    def write_form(self, username="", username_error="", password_error="", verify_password_error="", email="",
                   email_error=""):
        self.response.out.write(form % {
            "username": username, "username_error": username_error,
            "password_error": password_error,
            "verify_password_error": verify_password_error,
            "email": email, "email_error": email_error})

    def checkEscaped(text):
        return cgi.escape(text, quote="True")

    def valid_username(username):
        USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
        return USER_RE.match(checkEscaped(username))

    def valid_password(password):
        PASSWORD_RE = re.compile(r"^.{3,20}$")
        return PASSWORD_RE.match(checkEscaped(password))

    def valid_email(email):
        if len(email) > 0:
            EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
            return EMAIL_RE.match(checkEscaped(email))
        else:
            return True

    def password_match(password, confirmPassword):
        return password == confirmPassword

    def get(self):
        self.write_form()

    def post(self):
        input_username = self.request.get('username')
        input_password = self.request.get('password')
        input_verify_password = self.request.get('verify_password')
        input_email = self.request.get('email')

        username_error = ""
        is_valid_name = valid_username(input_username)
        if not is_valid_name:
            username_error = "That's not a valid username."

        password_error = ""
        is_valid_password = valid_password(input_password)
        if not is_valid_password:
            password_error = "That wasn't a valid password."

        verify_password_error = ""
        is_valid_verify_password = password_match(input_password, input_verify_password)
        if not is_valid_verify_password:
            verify_password_error = "the passwords don't match"

        email_error = ""
        is_valid_email = valid_email(input_email)
        if not is_valid_email:
            email_error = "that's not a valid email"

        if not (is_valid_name and is_valid_password and is_valid_verify_password and is_valid_email):
            self.write_form(name, username_error, password_error, verify_password_error, email, email_error)
        else:
            self.redirect("/welcome")


app = webapp2.WSGIApplication([
    ('/', MainPage), ('/welcome', WelcomeHandler)], debug=True)

You need to reference methods on the instance via the self object: 您需要通过self对象在实例上引用方法:

is_valid_password = self.valid_password(input_password)

You have the same problem in other places too; 您在其他地方也有同样的问题; checkEscaped() should be self.checkEscaped() , password_match() should be self.password_match() , etc. checkEscaped()应该是self.checkEscaped()password_match()应该是self.password_match()等。

Methods are not globals or locals. 方法不是全局变量或局部变量。

Next, your methods should accept a self reference too; 接下来,您的方法也应该接受一个self引用。 each of your own methods is missing the required parameter: 您自己的每个方法都缺少必需的参数:

def checkEscaped(self, text):
    # ...

def valid_username(self, username):
    # ...

def valid_password(self, password):
    # ...

def valid_email(self, email):
    # ...

def password_match(self, password, confirmPassword):
    # ...

Of course, you could just move those four methods out of the class and actually make them global functions instead of methods. 当然,您可以将这四个方法移出类,并实际上使它们成为全局函数而不是方法。 In that case, leave off the self arguments again. 在这种情况下,请再次取消self论证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM