-3

When using @classmethod this is passed first instead of self. Now inside the method with this decorator i need to call functions that are not defined inside this decorator but are defined in the class. How can i call the two functions get_int_input and get_non_int_input so that i can pass them to the return cls(name,pay_rate,hours) statement?

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)
Achilles
  • 195
  • 3
  • 12
  • why not just make it a method of the class, and why not use a for loop in range of 7 instead of your while loop? – Padraic Cunningham Jul 28 '14 at 22:54
  • `name` and `pay_rate` will also throw an error – Padraic Cunningham Jul 28 '14 at 22:56
  • 2
    Why on earth are you assigning e.g. `name` *outside* the class method? Why don't you use an input function for the hours? The input functions don't need to be class methods at all, and you haven't made them static or called them via `cls` like I told you you needed to if you left them in. And, also again, `hours` should be a class attribute. What's the point in asking questions if you ignore the answers? – jonrsharpe Jul 28 '14 at 22:56
  • 1
    Please read: http://stackoverflow.com/q/12179271/2359271 – Air Jul 28 '14 at 23:00
  • @jonrsharpe I'm not really sure what your intentions are but at this point your not helping and just harassing me. You said you were done helping so why are you here. I've been doing this for a month relax, im not ignoring anything you say im just not understanding and yes ive read the documentation several times. – Achilles Jul 28 '14 at 23:02

3 Answers3

1

You defined get_int_input and get_non_int_input inside the Employee class, which means that (by default) they should take an instance of Employee as the first argument. Your code is breaking that rule, which is probably the cause of problems.

Use @staticmethod decorator to indicate that get_int_input and get_non_int_input should not take an instance of Employee as the first argument.

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
1

You would add the @staticmethod decorator before the other two classes. Since they don't take either the Employee class or one of its instances as their first argument, they operate independently of a particular class or instance, and in this sense are "static".

A method decorated in this manner is an attribute of its containing class, and is called as a class attribute, for example:

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

This works the same way if you're calling Foo.bar() from inside one of Foo's class methods.

There are some other problems here, though - I would advise you to seek more comprehensive review and advice.

Air
  • 8,274
  • 2
  • 53
  • 88
  • Thanks so much, short and useful answer this solved one of my issues thanks man. Now i just need to pass whats returned by these functions to my return `cls(name,pay_rate,hours)` – Achilles Jul 28 '14 at 23:13
  • @Achilles Once you have your code working as you think it should, if you are interested in some thorough (and potentially brutal) feedback, you could try posting on [Code Review](http://codereview.stackexchange.com/). – Air Jul 28 '14 at 23:16
  • Thanks for the advice, I will definitely check that out i didn't even know there was such a thing. – Achilles Jul 28 '14 at 23:24
0

you seem to be missing some core concept of programming

you should probably look up namespaces and scope in google.

you should probably not talk down to john.r.sharp as he is very helpful and I would hazard a guess that if you continue programming you will have many many more problems that you come to SO for help with

all that said here is your fixed code

#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)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Pretty sure you have the talking down to part confused, i was the one being talked down to. I don't need to be harassed if someone doesn't want to contribute useful comments don't comment at all. Yes, he is helpful, that doesn't mean i have to sit back and take nonconstructive comments. – Achilles Jul 28 '14 at 23:29
  • Thank you for this though it is greatly appreciated, i have read plenty of documentation but when your new its not all that clear. This did help though thank you. – Achilles Jul 28 '14 at 23:30