How does derived class arguments work in Python?

2024/10/11 14:17:21

I am having difficulty understanding one thing in Python.I have been coding in Python from a very long time but there's is something that just struck me today which i struggle to understand

So the situation goes like this

I have a mixin and a view

class Mixin:def get_session(self,request,*args,**kwargs):print(self) #should be the instance passedprint(request) #should be the request object passed but it's also an instanceclass View:def get(self,request,*args,**kwargs):self.get_session(self,request,*args,*kwargs)pass

Why is the request argument the instance of the Class View, It should be request.Please help me clarify these concepts.

Answer

You're passing self explicitly as the first argument of get_session. That means it goes into the request parameter.

self.get_session(self,request,*args,*kwargs)^               ^        ^^^^^^^^^^
(self)        (request)    (the rest)

I think you mean:

self.get_session(request, *args, **kwargs)
https://en.xdnf.cn/q/118317.html

Related Q&A

grouping on tems in a list in python

I have 60 records with a column "skillsList" "("skillsList" is a list of skills) and "IdNo". I want to find out how many "IdNos" have a skill in common.How …

How do I show a suffix to user input in Python?

I want a percentage sign to display after the users enters their number. Thankspercent_tip = float(input(" Please Enter the percent of the tip:")("%"))For example, before the user t…

Discord.py Self Bot using rewrite

Im trying to make a selfbot using discord.py rewrite. Im encountering issues when attempting to create a simple command. Id like my selfbot to respond with "oof" when ">>>test&q…

int to binary python

This question is probably very easy for most of you, but i cant find the answer so far.Im building a network packet generator that goes like this:class PacketHeader(Packet): fields = OrderedDict([(&quo…

Get aiohttp results as string

Im trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio…

Waiting for a timer to terminate before continuing running the code

The following code updates the text of a button every second after the START button was pressed. The intended functionality is for the code to wait until the timer has stopped before continuing on with…

PySpark: how to resolve path of a resource file present inside the dependency zip file

I have a mapPartitions on an RDD and within each partition, a resource file has to be opened. This module that contains the method invoked by mapPartitions and the resource file is passed on to each ex…

Convert normal Python script to REST API

Here I have an excel to pdf conversion script. How can I modify it to act as a REST API? import os import comtypes.client SOURCE_DIR = D:/projects/python TARGET_DIR = D:/projects/python app = comtypes…

How to track changes in specific registry key or file with Python? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting …

How to use Android NDK to compile Numpy as .so?

Because the Numpy isnt a static library(it contains .py files, .pyc files, .so files, etc), so if I want to import it to my python code which is used in an Android phone(using CLE), I should recompile …