如何在@classmethod装饰器中使用函数

时间:2014-07-28 22:49:18

标签: python class python-2.7 class-method

使用@classmethod时,首先传递而不是self。现在在使用此装饰器的方法内部,我需要调用未在此装饰器内定义但在类中定义的函数。如何调用两个函数get_int_inputget_non_int_input以便我可以将它们传递给return cls(name,pay_rate,hours)语句?

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)

3 个答案:

答案 0 :(得分:1)

您在get_int_input类中定义了get_non_int_inputEmployee,这意味着(默认情况下)它们应该将Employee的实例作为第一个参数。您的代码违反了该规则,这可能是导致问题的原因。

使用@staticmethod装饰器指示get_int_inputget_non_int_input不应将Employee的实例作为第一个参数。

答案 1 :(得分:1)

您可以在其他两个类之前添加@staticmethod装饰器。由于他们不会将Employee类或其中一个实例作为第一个参数,因此它们独立于特定类或实例运行,并且在这个意义上是静态&#34;。

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

>>> class Foo(object):
...     @staticmethod
...     def bar():
...         print 'called the static method'
... 
>>> Foo.bar()
called the static method

如果您从Foo.bar()类方法中调用Foo,这种方法的工作方式相同。

但是这里还有其他一些问题 - 我建议你寻求更全面的评论和建议。

答案 2 :(得分:0)

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

你应该在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)