How to define decorator within the class?

shaila

I wanted to use decorator inside a class but I am getting positional error. 1.I dont want to use functools and import wraps. 2.Can we declare it within the class rather than defining decorator outside class? Is there any other method to declare a decorator within a class?

from pytube import YouTube

if __name__ == "__main__":
    class term :
        # def __init__(self,u,v):
        #     self.url = u
        #     self.path = v

        def check(func):
            def parameters(self,u,v):
                print(f"title:{YouTube(u).title}, views:{YouTube(u).views}, Length:{YouTube(u).length}")
                func(u,v)
                print("Yes done")
            return parameters
        # return check

        @check
        def video1(self,u,v):
            # y = YouTube(u)
            # print("Y is",y) 
            video1 = YouTube(u).streams.first().download(v)
            print("Success") 
            return video1

u = input("Enter the video URL: ")
v = input("Enter the path: ")

t1 = term()
t1.video1(u,v)
print(t1)

If I initialise u,v inside class and call it via t1 and use it in methods like check it gives an error saying term has positional argument error.

If I initialise u,v inside instance t1 and call it via t1.video1 and use it in methods like check it gives an error saying video has no positional argument "v".

How to use decorators?Can you pls help me.

Suyog Shimpi

Here is just an example for you, (Modify as per your need) -

class term:

    def check(func):
        def parameters(self, u, v):
            print(f"title:")
            print("Yes done")
            return func(self, u, v)
        return parameters

    @check
    def video1(self, u, v):
        print("Success")
        return 'Hello'

Also, You are printing an object like

t1 = term()
t1.video1(u,v)
print(t1)

But, you should be printing like -

t1 = term()
result = t1.video1(u,v)
print(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use a decorator of a descriptor within a sub class

How to define a class within another class in CLIPS?

How to define and play sound effects within the class

define a decorator as method inside class

Use class instance variable to define a button label within the decorator (discord.py 2.0.0)

Define predicate within class

How to use `@dataclass` decorator within a decorator?

How to define variables that will be used in every iteration of a function within a class in python?

how to use decorator in a class

Use an instance method as a decorator within another class

Implementing a decorator within class "major flaw"?

Attaching a decorator to all functions within a class

Get Class Instance from Within Decorator

python decorator - registering class within base

Hot to get the `this` from a class within it's decorator

How to define method to act as decorator for other methods inside the same class in Python?

Python decorator within function, needs class within method?

How can you define a Decorator interface in TypeScript

How to define angular's @Input decorator in an interface?

Python - Pass class as argument to decorator of method within that class?

Change the docstring of a class method within a decorator based on class attribute

How to use the Class decorator wrapper?

how to pass arguments to a decorator in a class

How create decorator inside a class?

How to apply a decorator to existing class

how to use decorator in class method

how to define java codeModel generated class within another generatede class WITHOUT fully qualifuied name

How to define constructor of fully-specialized class within another class template

How to register within a decorator all functions it decorates?