Python 如何在@classmethod decorator中使用函数

Python 如何在@classmethod decorator中使用函数,python,class,python-2.7,class-method,Python,Class,Python 2.7,Class Method,当使用@classmethod时,首先传递它,而不是self。现在,在这个decorator的方法中,我需要调用没有在这个decorator中定义但在类中定义的函数。如何调用这两个函数get\u int\u input和get\u non\u int\u input,以便将它们传递到return cls(姓名、工资率、小时数)语句 class Employee(object): def __init__(self,name,pay_rate,hours):

当使用@classmethod时,首先传递它,而不是self。现在,在这个decorator的方法中,我需要调用没有在这个decorator中定义但在类中定义的函数。如何调用这两个函数
get\u int\u input
get\u non\u int\u input
,以便将它们传递到
return cls(姓名、工资率、小时数)
语句

class Employee(object):

    def __init__(self,name,pay_rate,hours):        
        self.name = name
        self.pay_rate = pay_rate
        self.hours = ("mon","tues","wed","thursday","friday","saturday","sunday")

    def get_int_input(prompt):
        while True:
            pay_grade = raw_input(prompt)
            try:
                i = int(pay_grade)
            except ValueError:
                print "Int Only"
            else:
                return i

    def get_non_int_input(prompt):
        while True:
            a_name = raw_input(prompt)
            try:
                i = int(a_name)
            except ValueError:
                return a_name
            else:
                print " Strings Only"

    @classmethod
    def from_input(cls):

        day_count = 1
        hours = ("m","tue","w","thur","f","s","sun")
        while day_count <= 7:
            for day in hours:
                day = input("  Enter hours for day " + str(day_count) + "--- ")
                day_count += 1     


        return cls(name,pay_rate,hours)
     name = get_non_int_input("\n  Enter new employee name\n")
     pay_rate = get_int_input("Enter pay rate  ")


employee = Employee.from_input()
print str(employee)
类员工(对象):
定义初始(自我、姓名、工资率、小时数):
self.name=名称
自付率=自付率
self.hours=(“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”)
def get_int_输入(提示):
尽管如此:
付款等级=原始输入(提示)
尝试:
i=整数(薪级)
除值错误外:
打印“仅限整数”
其他:
返回i
def get_非_int_输入(提示):
尽管如此:
a\u名称=原始输入(提示)
尝试:
i=int(a_名称)
除值错误外:
返回一个名称
其他:
“仅打印字符串”
@类方法
来自_输入(cls)的def:
天数=1
小时数=(“上午”、“星期二”、“下午”、“星期四”、“下午”、“下午”、“下午”、“下午”、“下午”、“下午”、“星期日”)

而day\u count您在
Employee
类中定义了
get\u int\u input
get\u non\u int\u input
,这意味着(默认情况下)它们应该将
Employee
的一个实例作为第一个参数。您的代码违反了该规则,这可能是问题的原因


使用
@staticmethod
decorator指示
get\u int\u input
get\u non\u int\u input
不应将
Employee
的实例作为第一个参数。

您将在其他两个类之前添加
@staticmethod
decorator。由于它们不将
Employee
类或其实例之一作为第一个参数,因此它们独立于特定的类或实例运行,从这个意义上说是“静态的”

以这种方式修饰的方法是其包含类的属性,并被称为类属性,例如:

>>> class Foo(object):
...     @staticmethod
...     def bar():
...         print 'called the static method'
... 
>>> Foo.bar()
called the static method
如果您从
Foo
的一个类方法内部调用
Foo.bar()
,则其工作方式相同


不过,这里还有一些其他问题-我建议您寻求更全面的审查和建议。

您似乎缺少一些编程的核心概念

您可能应该在google中查找名称空间和范围

您可能不应该贬低john.r.sharp,因为他非常乐于助人,我冒昧地猜测,如果您继续编程,您将遇到更多问题,因此需要帮助

这里说的都是你的固定密码

#first pull these two functions out of your class they have nothing to do with employee
#they should just be normal functions #
#... if you wanted to make them part of a class make an input class and add them as static methods to that
def get_int_input(prompt):
    while True:
        pay_grade = raw_input(prompt)
        try:
            i = int(pay_grade)
        except ValueError:
            print "Int Only"
        else:
            return i

def get_non_int_input(prompt):
    while True:
        a_name = raw_input(prompt)
        try:
            i = int(a_name)
        except ValueError:
            return a_name
        else:
            print " Strings Only"

class Employee(object):    
    def __init__(self,name,pay_rate,hours):        
        self.name = name
        self.pay_rate = pay_rate
        self.hours = ("mon","tues","wed","thursday","friday","saturday","sunday")

    @classmethod
    def from_input(cls):
        day_count = 1
        hours = ("m","tue","w","thur","f","s","sun")
        while day_count <= 7:
            for day in hours:
                day = input("  Enter hours for day " + str(day_count) + "--- ")
                day_count += 1   
        #name and pay_rate must be defined prior to using them in your return function ...
        name = get_non_int_input("\n  Enter new employee name\n")
        pay_rate = get_int_input("Enter pay rate  ")
        #now that you have all the info just return it
        return cls(name,pay_rate,hours)



employee = Employee.from_input()
print str(employee)
#首先将这两个函数从类中拉出,它们与employee无关
#它们应该只是正常功能#
#... 如果您想使它们成为类的一部分,请创建一个输入类,并将它们作为静态方法添加到该类中
def get_int_输入(提示):
尽管如此:
付款等级=原始输入(提示)
尝试:
i=整数(薪级)
除值错误外:
打印“仅限整数”
其他:
返回i
def get_非_int_输入(提示):
尽管如此:
a\u名称=原始输入(提示)
尝试:
i=int(a_名称)
除值错误外:
返回一个名称
其他:
“仅打印字符串”
类别雇员(对象):
定义初始(自我、姓名、工资率、小时数):
self.name=名称
自付率=自付率
self.hours=(“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”)
@类方法
来自_输入(cls)的def:
天数=1
小时数=(“上午”、“星期二”、“下午”、“星期四”、“下午”、“下午”、“下午”、“下午”、“下午”、“下午”、“星期日”)

虽然day_count为什么不将其作为类的一个方法,为什么不使用范围为7的for循环而不是您的while循环?
name
pay_rate
也会抛出一个错误,您到底为什么要在类方法之外指定,例如
name
?你为什么不在小时数上使用输入功能呢?输入函数根本不需要是类方法,也没有像我告诉过你的那样,让它们成为静态的,或者通过
cls
调用它们,如果你把它们放在里面的话。同样,
hours
应该是类属性。如果你忽略了答案,问问题有什么意义?请阅读:@jornsharpe我不确定你的意图是什么,但在这一点上你没有帮助我,只是骚扰我。你说你已经帮了那么你为什么在这里。我已经做了一个月了放松,我不会忽略你说的任何话,我只是不理解,是的,我已经阅读了好几次文档。非常感谢,简短而有用的回答解决了我的一个问题谢谢。现在,我只需要将这些函数返回的内容传递给我的return
cls(姓名、薪资、小时数)
@achiles一旦您的代码按您认为的方式运行,如果您对一些彻底(可能是残酷的)反馈感兴趣,您可以尝试发布。谢谢您的建议,我一定会查清楚,我甚至都不知道有这样的事情。很确定你把谈话的重点搞糊涂了,我就是被谈话的那个人。如果有人不想发表有用的评论,我不需要被骚扰,完全不要评论。是的,他很有帮助,但这并不意味着我必须坐下来接受非建设性的评论。谢谢你,尽管我非常感激,我已经阅读了大量的文档,但当你的新文档不那么清晰时。不过这确实有帮助,谢谢你。