self

How to remove the self argument from a decorator's arguments if there is one

How to remove the self argument from a decorator's arguments if there is one Question: Suppose I have a simple decorator, and a classes method and a function which I both decorate with that decorator: import functools def decorator(func): @functools.wraps(func) def call(*args): print(args) func(*args) return call class cls: @decorator def meth(self, a): pass @decorator def …

Total answers: 2

How to create routes with FastAPI within a class

How to create routes with FastAPI within a class Question: So I need to have some routes inside a class, but the route methods need to have the self attr (to access the class’ attributes). However, FastAPI then assumes self is its own required argument and puts it in as a query param This is …

Total answers: 9

What am I not understanding about 'self' instance?

What am I not understanding about 'self' instance? Question: Here is a link to my github: airplane.py I keep receiving the error: NameError: name ‘self’ is not defined I have looked through countless other stack overflow threads and there is clearly something I am not understanding about the self instance, because it feels like I’ve …

Total answers: 2

Naming the self parameter something else

Naming the self parameter something else Question: In Python, this code is valid: class A: def __init__(me): me.foo = 17 def print_foo(myself): print(myself.foo) def set_foo(i, v): i.foo = v As you might already have noticed, the self parameter is named me in the __init__ method, myself in the print_foo method and i in the set_foo …

Total answers: 4

How can I decorate an instance method with a decorator class?

How can I decorate an instance method with a decorator class? Question: Consider this small example: import datetime as dt class Timed(object): def __init__(self, f): self.func = f def __call__(self, *args, **kwargs): start = dt.datetime.now() ret = self.func(*args, **kwargs) time = dt.datetime.now() – start ret[“time”] = time return ret class Test(object): def __init__(self): super(Test, self).__init__() …

Total answers: 2

TypeError: method() takes 1 positional argument but 2 were given

"TypeError: method() takes 1 positional argument but 2 were given" but I only passed one Question: If I have a class … class MyClass: def method(arg): print(arg) … which I use to create an object … my_object = MyClass() … on which I call method("foo") like so … >>> my_object.method("foo") Traceback (most recent call last): …

Total answers: 12

Difference between Python self and Java this

Difference between Python self and Java this Question: I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python “self” method and Java “this”. I know that “self” is not a keyword while “this” is. And that …

Total answers: 4

What is the purpose of checking self.__class__?

What is the purpose of checking self.__class__? Question: What is the purpose of checking self.__class__ ? I’ve found some code that creates an abstract interface class and then checks whether its self.__class__ is itself, e.g. class abstract1 (object): def __init__(self): if self.__class__ == abstract1: raise NotImplementedError(“Interfaces can’t be instantiated”) What is the purpose of that? …

Total answers: 5

Is it okay to pass self to an external function

Is it okay to pass self to an external function Question: I have a class, A, which is inherited by a bunch of other classes. Some of these have a few functions which are similar and it would be nice to have those functions defined somewhere else and called by the classes that need them. …

Total answers: 1

How to keep track of class instances?

How to keep track of class instances? Question: Toward the end of a program I’m looking to load a specific variable from all the instances of a class into a dictionary. For example: class Foo(): def __init__(self): self.x = {} foo1 = Foo() foo2 = Foo() … Let’s say the number of instances will vary …

Total answers: 7