0
def make_sequence(seq):
    def filter1(*func):
        new_filter = filter(func, seq)
        return tuple(new_filter)

    def filter_iterator(*func):
        new_seq = tuple(filter(func, seq))
        index = 0

        def next1():
            nonlocal index
            if index >= 0 and index < len(new_seq):
                ele = new_seq[index]
                index += 1
            else:
                index = 0
                ele = new_seq[index]
                index += 1
            return ele

        def reverse():
            nonlocal index
            index -= 1
            if index >= 0 and index < len(new_seq):
                ele = new_seq[index]
            else:
                index = len(new_seq) - 1
                ele = new_seq[index]
            return ele

        return {'next': next1, 'reverse': reverse}

    def reverse():
        return tuple(seq[::-1])

    def extend(new_seq):
        nonlocal seq
        seq += new_seq

    return {'filter': filter1, 'filter_iterator': filter_iterator, 'reverse': reverse, 'extend': extend}


s1 = make_sequence((1, 2, 3, 4, 5))
print(s1['filter'](lambda x: x % 2 == 0))

When I try to return tuple from fitlter1 I get the error:

'tuple' object is not callable.

What I try to do is the filter1 get the argument *args because when I will call filiter1() with nothing function he returns the sequence without change.

1 Answer 1

1

I removed the * on your def filter1(func): try that, I noticed that your parameter is a *func with * where in fact it is a tuple when you put some arguments on the function, It seems that you do not need the *. I think you need to search about *args and **kwargs

I've run it and got this output

(2, 4)
[Finished in 0.3s]

Have a quick sample here why you got that message. Because when you put * to a parameter in a function it's data type is a tuple.

def foo(*args):
    print(args)
    print(type(args))


foo(1)

Output:

(1,)
<class 'tuple'>

Why you are getting the error?

filter(func, seq)

The filter function is expecting that your func is a function or at least lambda but it's not, what it actually is , is a tuple once you put * on your def filter1(*func).


So base on what you've said here.

What I try to do is the filter1 get the argument *args because when I will call filiter1() with nothing function he returns the sequence without change.

What you can do with your function is

def filter1(func=None):
    if callable(func):
        new_filter = filter(func, seq)
        return tuple(new_filter)

    return seq

Change your filter1 function to that and you'll get this.

print(s1['filter'](lambda x: x % 2 == 0))
print(s1['filter']())
print(s1['filter'](lambda x: x>2))

Output:

(2, 4)
(1, 2, 3, 4, 5)
(3, 4, 5)
Sign up to request clarification or add additional context in comments.

5 Comments

yeah i know without * it will work but when i try to do print(s1['filter']()) it will say filter1() missing 1 required positional argument: 'func'
Yes it will throw that error and the reason because in your function filter1 you have a parameter there or in other words an argument that you need to provide on. As you said on your code here print(s1['filter']()) you are not providing any, the () is blank so you need to provide there a function since the parameter there as I see is you're going to put it on the filter function.
i understand why i get error but the mission i need to do is : when i call the key of 'filter' i need to send function to filter my seq or not send noting to filter1 and it not change my seq
You need to do what? have *? object is not callable means tuple is not callable so it's not a function.
for example : s1 = make_seq((1,2,3,4,5)), >>> s1['filter'](lambda x: x>2) >>>(3,4,5) >>>s1['filter']() >>>(1,2,3,4,5)

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.