PTP_XX
PTP_XX

Reputation: 59

how to fix TypeError: class() takes no arguments problem?

I am trying to create a class name RangePrime and it's instance attributes should print out the range of them and finally appear an issue. How to fix them?

class RangePrime:
    def range_list(self,a,b):
        self.a = a
        self.b = b
        x = []
        if b < a:
            x.extend(range(b, a))
            x.append(a)
        else:
            x.extend(range(a, b))
            x.append(b)
        return x

after i ran this -> range_prime = RangePrime(1, 5) and it start to appear

TypeError: RangePrime() takes no arguments

I should get the following result:

>>> range_prime = RangePrime(1, 5)
>>> range_prime.range_list
[1, 2, 3, 4, 5]

Upvotes: 1

Views: 9645

Answers (3)

citynorman
citynorman

Reputation: 5292

I had a typo and did __int__ instead of __init__

Upvotes: -1

justjokingbro
justjokingbro

Reputation: 167

create object of the class first and then pass those parameter in your class method then it will work

ob=RangePrime()

print(ob.range_list(1,5))

Upvotes: 0

Celius Stingher
Celius Stingher

Reputation: 18367

It looks you have mixed using functions without a class definition and at the same time misdefining the functions within the class by lacking an __init__(). I have modified your code a bit to account for the class and its functions, so the intention of your code remains the same. Kindly try:

class RangePrime():
    def __init__(self, a,b):
        self.a = a
        self.b = b
        
    def range_list(self):
        a = self.a
        b = self.b
        x = []
        if b < a:
            x.extend(range(b, a))
            x.append(a)
        else:
            x.extend(range(a, b))
            x.append(b)
        return x

Which when running:

range_prime = RangePrime(1, 5)
range_prime.range_list()

Returns:

[1,2,3,4,5]

Upvotes: 1

Related Questions