Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: FUN instance has no attribute '__trunc__'

Tags:

python

numpy

here is my class FUN, but the result is showing AttributeError: FUN instance has no attribute '__trunc__'. please tell me where is the error in coding and what to modify.

import math
import random
import string
import numpy as np
import pickle
from itertools import chain

random.seed(0)

class FUN:
 def __init__(self):
    print "fun"
 # set a random numbers between a & b
 def rand(a, b):
    self.rand = (b-a)*random.random() + a
    return self.rand

 # sigmoid function, tanh ~ 1/(1+e^-x)
 def sigmoid(x):
     self.sig = math.tanh(x)
     return self.sig

 def sigmoid1(x):
    self.sig1 = 1/(1+math.exp(-x))
    return self.sig1

 # derivative of sigmoid function, in terms of the output (y)
 def dsigmoid(y):
    self.dsig = 1.0 - y**2
    return self.dsig
 # getting 2d array
 #def matrix(I, J, fill=0.0):
    #return [[val for col in range(I)] for row in range(J)]
 #obtain a matrix
 def matrix(I, J, fill=0.0):
    m = []
    for i in range(I):
        m.append([fill]*J)
    return m
f = FUN()
print f.matrix(2,3)

the above code is giving the following type of errors:

fun
Traceback (most recent call last):
File "functions.py", line 42, in <module>
 print f.matrix(2,3)
File "functions.py", line 38, in matrix
 for i in range(I):
AttributeError: FUN instance has no attribute '__trunc__'

please help me to resolve this error.

like image 692
lkkkk Avatar asked Feb 12 '26 14:02

lkkkk


1 Answers

Methods take a self argument as the first parameter; you named it I instead and passed that to range(), which then tries to turn the instance of your FUN custom class into a integer number. That failed:

>>> class FUN: pass
... 
>>> range(FUN())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: FUN instance has no attribute '__trunc__'

Add a self argument:

def matrix(self, I, J, fill=0.0):

You'll need to do this for the other methods as well; all but the __init__ method are missing self.

Python tries object.__int__ first, then tries object.__trunc__, a method surprisingly underdocumented. Only the math.truncate() documentation and the Numbers Type Hierarchy PEP 3141 proposal mention the method.

like image 94
Martijn Pieters Avatar answered Feb 14 '26 03:02

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!