How do I write a class to make this code work.
class number:
def double(self):
return n*2
print(number(44).double)
>> 88
Well, you could decorate the number.double method with property:
class number:
def __init__(self, number):
self.number = number
@property
def double(self):
return self.number * 2
print(number(42).double) # 84
If you know the type of your argument, it'd be better to inherit number from it. For example
class number(int):
@property
def double(self):
return type(self)(self * 2)
print(number(42).double) # 84
print(number(42).double.double) # 168
number('Too generic huh?').doubleint, float, long, and complex.Here you are:
class Number(object):
def __init__(self, n):
self.n = n
def double(self):
return 2*self.n
print(Number(44).double())
A couple of notes:
double() is a method of the class Number (and not an attribute), you need to use parentheses to call it.n, or 44, you must def the __init__() function which gives the interpreter instructions for how to create, or initialize an instance of your class.Hope this helps, and good luck!
test.assert_equals(). I'm almost positive that test.assert_equals() takes the actual function as an argument (rather than the value returned by calling the function), so that it can call the function itself. If this is so, then PrimeFactorizer.factor() is really a method even though you don't use parentheses when using it that particular way.