Consider this code:
class SomeClass:
def __init__(self, i):
self.i = i
def some_method(self):
def returned_method(self, new_i):
self.i = new_i
return returned_method
some_obj = SomeClass(5)
some_obj.some_method()(6)
print(some_obj.i)
It ends with an exception:
Traceback (most recent call last):
File "./prog.py", line 11, in <module>
TypeError: returned_method() missing 1 required positional argument: 'new_i'
Clearly, the method returned by some_method is unbound. It doesn't get some_obj as its first argument.
How to bind returned_method in some_method to self?
returned_method isn't a method; it's an ordinary function. There's no need to declare it as taking two arguments.
def some_method(self):
def _(new_i):
self.i = new_i
return _
_ (no need to give it any particular name) doesn't need self as an argument, because it's a closure over the argument passed to some_method.
It gets used the same way as before:
some_obj = SomeClass(5)
some_obj.some_method()(6)
print(some_obj.i)
Related
Part A
I want to do some checking on arguments to a class instantiation and possibly return None if it doesn't make sense to even create the object.
I've read the docs but I don't understand what to return in this case.
class MyClass:
def __new__(cls, Param):
if Param == 5:
return None
else:
# What should 'X' be?
return X
What should X be in return X?
It cannot be self because the object doesn't exist yet so self is not a valid keyword in this context.
Part B
Tied to my question, I don't understand the need to have the cls parameter.
If you call the constructor of MyClass - var = MyClass(1) - won't cls always be MyClass?
How could it be anything else?
According to the docs, cls in object.__new__(cls[, ...]) is:
. . .the class of which an instance was requested as its first
argument.
(I'm assuming you are using Python 3 because you provided a link to Python 3 docs)
X could be super().__new__(cls).
super() returns the parent class (in this case it is simply object). Most of the times when you are overriding methods you will need to call the parent class's method at some point.
See this example:
class MyClass:
def __new__(cls, param):
if param == 5:
return None
else:
return super().__new__(cls)
def __init__(self, param):
self.param = param
And then:
a = MyClass(1)
print(a)
print(a.param)
>> <__main__.MyClass object at 0x00000000038964A8>
1
b = MyClass(5)
print(b)
print(b.param)
>> None
Traceback (most recent call last):
File "main.py", line 37, in <module>
print(b.param)
AttributeError: 'NoneType' object has no attribute 'param'
You could just return the instance of cls like this return object.__ new__(cls). Because every class is subclass of object, you can use that as a object creator for your class. The returnes object is passed as a first argument to the __init__() with the any number of positional argument or any number of keyword argument you passed to new. There you will create instance variable assigning those values.
I made a constant decorated that looks like this
def constant(f):
def fset(self, value):
raise TypeError
#wraps(f)
def fget(self):
return f()
return property(fget, fset)
but when I use it on a function and call it, I get thrown a type error for not passing in enough arguments, I thought adding in the wraps decorator would solve this but it didn't.
class IrrigationAmount(AbstractCalculation):
def __init__(self, wvf, fc):
self._wvf = float(wvf)
self._fc = float(fc)
self._value = 0
def calculate(self):
self._value = (self.fc*SECTORVOLUME - self.wvf*SECTORVOLUME)/FLOWRATE
#constant
def value(self):
return self._value
Here's how I tested it, I don't understand what I'm doing wrong, mainly why self isn't being passed in automatically due to the wraps. I probably misunderstood what wraps does terribly.
>>> from irrigationservice.irrigation import IrrigationAmount
>>> a = IrrigationAmount(0.12, 0.2)
>>> a.calculate()
>>> a.value
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/krishna/Documents/irrigationwebserver/webserver/irrigationservice/utils.py", line 12, in fget
return f()
TypeError: value() takes exactly 1 argument (0 given)
I think you want to replace the constant function definition and the #constant decorator with something like this:
#property
def value(self):
"""return the protected value"""
return self._value
EDIT: add the following example to address some comments
Create file myvalue.py...
class MyValue:
def __init__(self, x, y):
self._x = x
self._y = y
self._value = 0
def calculate(self):
self._value = self._x*self._y
#property
def value(self):
"""return the protected value"""
return self._value
Now run in python:
>>> from myvalue import MyValue
>>> z = MyValue(2, 3)
>>> z.value
0
>>> z.calculate()
>>> z.value
6
>>> z.value = 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
While Steve's answer is the right way to do this (you don't need a special #constant decorator when #property is naturally read-only unless you explicitly decorate other methods with #propname.setter), the reason you have the problem is that the decorated class method isn't bound, and you're not explicitly passing self to it.
Change the code to:
def fget(self):
return f(self)
so the self argument actually goes where it's supposed to.
How to I declare a default value in a python object?
Without a python object it looks fine:
def obj(x={123:'a',456:'b'}):
return x
fb = obj()
print fb
With a python object I get the following error:
def foobar():
def __init__(self,x={123:'a',456:'b'}):
self.x = x
def getStuff(self,field):
return x[field]
fb = foobar()
print fb.x
Traceback (most recent call last):
File "testclass.py", line 9, in <module>
print fb.x
AttributeError: 'NoneType' object has no attribute 'x'
How do I get the object to return the value of a variable in the object?
With a python object, I got an error:
def foobar():
def __init__(self,x={123:'a',456:'b'}):
self.x = x
def getStuff(self,field):
return x[field]
fb2 = foobar({678:'c'})
print fb2.getStuff(678)
Traceback (most recent call last):
File "testclass.py", line 8, in <module>
fb2 = foobar({678:'c'})
TypeError: foobar() takes no arguments (1 given)
You didn't define a class, you defined a function with nested functions.
def foobar():
def __init__(self,x={123:'a',456:'b'}):
self.x = x
def getStuff(self,field):
return x[field]
Use class to define a class instead:
class foobar:
def __init__(self,x={123:'a',456:'b'}):
self.x = x
def getStuff(self, field):
return self.x[field]
Note that you need to refer to self.x in getStuff().
Demo:
>>> class foobar:
... def __init__(self,x={123:'a',456:'b'}):
... self.x = x
... def getStuff(self, field):
... return self.x[field]
...
>>> fb = foobar()
>>> print fb.x
{456: 'b', 123: 'a'}
Do note that using a mutable value for a function keyword argument default is generally not a good idea. Function arguments are defined once, and can lead to unexpected errors, as now all your classes share the same dictionary.
See "Least Astonishment" and the Mutable Default Argument.
to define a class in python you have to use
class classname(parentclass):
def __init__():
<insert code>
With your code you're declaring a method not a class
Use
class foobar:
instead of
def foobar():
I ran the code below, by calling the function in the constructor
First --
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Chaitanya')
C
h
a
i
t
a
n
y
a
Once again I run this and I get this
>>> class PrintName:
... def __init__(self, value):
... self._value = value
... printName(self._value)
... def printName(self, value):
... for c in value:
... print c
...
>>> o = PrintName('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
NameError: global name 'printName' is not defined
Can I not call a function in the constructor? and whay a deviation in the execution of similar code?
Note: I forgot to call a function local to the class, by using self (ex: self.printName()). Apologize for the post.
You need to call self.printName since your function is a method belonging to the PrintName class.
Or, since your printname function doesn't need to rely on object state, you could just make it a module level function.
class PrintName:
def __init__(self, value):
self._value = value
printName(self._value)
def printName(value):
for c in value:
print c
Instead of
printName(self._value)
you wanted
self.printName(self._value)
It probably worked the first time because you had another function printName in a parent scope.
What you want is self.printName(self._value) in __init__, not just printName(self._value).
I know this is an old question, but I just wanted to add that you can also call the function using the Class name and passing self as the first argument.
Not sure why you'd want to though, as I think it might make things less clear.
class PrintName:
def __init__(self, value):
self._value = value
PrintName.printName(self, self._value)
def printName(self, value):
for c in value:
print(c)
See Chapter 9 of the python manuals for more info:
9.3.4. Method Objects
Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.
I have a method (__init__) in a class, and I want to use a function from the class in this method.
But when I want to run my program. I get: NameError: global name 'myfunction' is not defined
Someone, who knows what I have to do? :)
Thank you. But I have still a problem, because def myFunc(self, a): is a method and I wanted a function.
class Myclass(object):
def __init__(self, a):
self.a = self.myFunc(a)
def myFunc(self, a):
return a+1
Then you don't have a function call in the method, but you have a method call in it.
When creating a class you must specify the object when calling its methods:
>>> class A(object):
... def __init__(self, val):
... self.val = self._process(val)
... def _process(self, val):
... return val % 7
... process = _process #if you are outside methods then you don't
... #have to add "self.".
...
>>> a = A(5)
>>> a.process(3)
3
>>> a._process(6) #"a" is passed as the "self" parameter
6
As you can see in a class definition, but outside the methods you must specify the method name only, and not the "self.". Also you can't refer to a method not already defined:
>>> class B(object):
... def __init__(self):pass
... def method1(self):pass
... __call__ = method2 #method2 not defined!
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in B
NameError: name 'method2' is not defined