Python 3.6 - 自我识别为参数?

时间:2017-12-05 21:38:50

标签: python class oop

我正在编写一个用户保护程序,我有5个类,当我从其中一个运行一个函数时,它会显示TypeError: g is not defined,但我输入了正确的参数。它认为自我是参数。我当然认为必须有一个解决方案。

class Generator(object):
    """Generates data base
    class."""

    def __init__(self):
        """type 0 = user
        type 1 = password"""
        pass

    def generate(self, formats, userType, fn, sn, a, c, g):

        empty_data = formats

        if userType == 0:

            empty_data["foreName"] = fn
            empty_data["surName"] = sn
            empty_data["age"] = a
            empty_data["country"] = c
            empty_data["gender"] = g

        else:

            pass

            return empty_data



class UserGenerator(object):
"""Generates new user data
in json format."""

    def __init__(self):
    """New user data gen"""

        self.userFormat = userFormat = {

            "foreName": None,
            "surName": None,
            "age": None,
            "country": None,
            "gender": None,
        }



    def generateUser(self, fn, sn, a, c, g):
        """Returns a dict with usr data"""

        gen = Generator()

        userDataDict = gen.generate(self.userFormat, 0, fn, sn, a, c, g)

        return userDataDict


    class User(object):
        """defines a new base User"""

       def __init__(self):
        """Data is a dictionary that is
        the information for a new user
        userdata is stored as json."""

            pass

       def saveUser(self, data):

        self.foreName = data["foreName"]
        self.surName = data["surName"]
        self.age = data["age"]
        self.country = data["country"]
        self.gender = data["gender"]

        self.user = UserGenerator.generateUser(self.foreName, self.surName, 
        self.age, self.country, self.gender)

        data = self.data

        Data.save(data, "userData.json")

        user = User()
        ug = UserGenerator()


 def newUser(fn, sn, a, c, g):
"""Creates a new base user
and saves it."""

    data = ug.generateUser(fn, sn, a, c, g)

    user.saveUser(data)

除了显而易见的缩进错误,我为什么要

Traceback (most recent call last):
  File "user.py", line 125, in <module>
    newUser("John", "Doe", 21, "Random", "Random")
  File "user.py", line 118, in newUser
    user.saveUser(data)
  File "user.py", line 102, in saveUser
    self.user = UserGenerator.generateUser(self.foreName, self.surName, self.age, self.country, self.gender)
TypeError: generateUser() missing 1 required positional argument: 'g'

我该如何解决这个问题?我如何阻止它认为自我是一个参数?

0 个答案:

没有答案