在python中应用OOPM时出错

时间:2017-11-15 16:35:27

标签: python

首先,我是python编程的新手。 这是我的代码: -

class login_ok(object):
    """docstring for instalogin"""
 def __init__(self):
 pass

def open_website(self,websiteName):
print(websiteName)


def login(self,email,password):
print(email)



def search(self,searchVariable):
print(searchVariable)


login1 = login_ok()
web = "a"
email_pass = "c"
password_pass = "b"
search_variable = "d"
login1.open_website(web)
login1.login(email_pass,password_pass)
login1.search(search_variable)

我正在使用sublime,我收到此错误'login_ok'对象没有属性'open_website'。

2 个答案:

答案 0 :(得分:3)

欢迎来到Python世界。

你有一些基本的缩进问题,这是Python工作原理的基础。我建议你通过一些教程和学习阶段来帮助你掌握基础知识。

这是一个开始的好地方的链接: http://docs.python-guide.org/en/latest/intro/learning/

举例来说,这里只是几个固定错误的例子:

class login_ok(object):
    """docstring for instalogin"""
    def __init__(self):
        # For example, all the code defined within a method or function is indented.
        pass

    # Similarly, all methods defined 'within' a class need to be indented four spaces.
    def open_website(self,websiteName):
        # Through proper indentation, you're telling the Python interpreter
        # that `open_website` is a method of the parent `login_ok` class
        print(websiteName)

答案 1 :(得分:0)

在定义类时,您将其方法定义为其范围之外的无关函数。为了使您的方法在您的类的范围内,它们需要编写如下:

class login_ok(object):
    """docstring for instalogin"""
    def __init__(self):
        pass

    def open_website(self,websiteName):
        print(websiteName)

    def login(self,email,password):
        print(email)

    def search(self,searchVariable):
        print(searchVariable)

您可以使用制表符或空格缩进,但必须保持一致且不要混合它们。您可能希望阅读有关Variables and ScopeClasses的更多文档,以便更好地了解如何构建程序。编写Python的样式与其功能直接相关,因为缺少大括号意味着范围由缩进定义。