2
class idk():
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def t(self):
        self.t = self.name + self.age
    def qrt(self):
        print(len(self.t))                             

abc = idk('abc','19')                 
abc.qrt()

abc.name= 'aduhd'            
abc.qrt()

When i run this code i get the following error

Traceback (most recent call last):
    File "C:/Users/Prajval/Desktop/test.py", line 11, in <module>
        abc.qrt()
    File "C:/Users/Prajval/Desktop/test.py", line 8, in qrt
        print(len(self.t))
TypeError: object of type 'instancemethod' has no len()

what does the following error mean:

TypeError: object of type 'instancemethod' has no len()

1
  • It gives the error because variable 't' does not exist Commented Sep 28, 2017 at 14:44

2 Answers 2

1

You forgot to call the method, which you have given the same name t as an instance variable.

def qrt(self):
    print(len(self.t()))

By naming a method the same as an instance variable you are playing with fire. First time when you call the t method, it would behave as a method, and as a string post first call. Don't dug a hole for yourself to fall in.

Sign up to request clarification or add additional context in comments.

Comments

1

Final code,

class idk():
    def __init__(self,name,age):
        self.name = name
        self.age = age
        
    def t(self):
        self.t1 = self.name + self.age
        return self. t1 # was missing 
        
    def qrt(self):
        print(len(self.t()))                             

abc = idk('abc','19')                 
abc.qrt()

abc.name= 'aduhd'            
abc.qrt()
5
7

[Program finished]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.