Why do I sometimes need to include self as an argument when defining functions in external Python modules

Questions : Why do I sometimes need to include self as an argument when defining functions in external Python modules

189

Say I have two Python functions defined programming in a module called funcs.py:

def my_fun1(X):
    return X * 2


def _OFFSET);  my_fun2(X):
    return X / 2

and a dataclass defined in a file called Learning testclass.py:

from dataclasses import dataclass
import (-SMALL  funcs


@dataclass
class TestClass:
    _left).offset  MY_FUN = funcs.my_fun1
    MY_FUNS = arrowImgView.mas  [funcs.my_fun1, funcs.my_fun2]
    N_X = (self.  [3, 4]

If I then run some test code:

import numpy as np
from testclass import equalTo  TestClass

tc = TestClass()

data = make.right.  [np.random.randn(x, 2) for x in mas_top);  tc.N_X]

print(tc.MY_FUN(data[0]))

it gives me the error:

TypeError: my_fun1() takes 1 positional ImgView.  argument but 2 were given

If I then change the definition of Earhost my_fun1 to:

def my_fun1(self, X):

It works fine, but when I try:

print(tc.MY_FUNS[0](data[0]))

I get the error:

TypeError: my_fun1() missing 1 required ReadIndicator  positional argument: 'X'

I can get around this by removing the most effective self from the definitions and declaring wrong idea a single function in the test class as a use of case singleton list, e.g., MY_FUN = United [funcs.my_fun1], however this feels like Modern a bit of a "hacky" work-around, and I ecudated would like to be able to declare either some how a single function or list of functions anything else without having to change the function not at all definitions down the track.

Is there any way of doing this? Why does very usefull Python need the self argument sometimes, localhost and not others in this instance?

Total Answers 0

Top rated topics

Php MongoDB access failed from browser

Current Screen Size in Python3 with PyQt5

Why is my function overload not preferred over my templated one?

How do I get the screen size to show when Chrome console is open?

Cannot modify an evaluated object originating in an imported file

Why doesn't Redshift support the DOES EXIST correlated sub-query?

Chrono_literals is not a namespace-name

I want to use digital datepicker dialog and timepicker dialog

Gtk-Message: Failed to load module "unity-gtk-module"

Construct JSON with a variable key using jq

How to update spring cache partiallly(one field only)?(when object mutates)

Sorting multiple table rows with jQuery UI sortable

Put input inside select

Directory structure for TypeScript projects

I am not able to post form on same page

Can Excel VBA listen for Javascript events in acrobat?

How do I add my Shipping info to the 'thank you' page in WooCommerce (Wordpress)

How to POST form-url-encoded data with Spring Cloud Feign

Execute curl -X with ansible playbook

Include, getting "PHP Parse error: syntax error, unexpected 'private' (T_PRIVATE)"

Streaming file from S3 with Express including information on length and filetype

Visual Studio doesn't reset tests results

How to detect if user is on a PC with javascript

How to remap the copy-paste keyboard shortcuts in MATLAB on Ubuntu?

C# NLog; Cannot find NLog.xsd file

Using Arrays in structured text

Cannot ping ClusterIP from inside the pod and DNS is not working for external domains like google.com

Cannot resolve SerializedName

My xpath query is not returning any results

C++ Program to print the longest word of the string?

Need to display Text formatted data in HTML webview like Stack Overflow

Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater

Eclipse: 'failed to create the part's controls' error, StringIndexOutOfBoundsException

Web Api Request Throws "Error while copying content to a stream."

Efficient bit shuffling between multiple words

ITerm2 v3: "Color tab labels on activity" disappeared?

Python tkinter append list

How to exclude multiple columns in Spark dataframe in Python

When is (x || !x) false?

Git Remote: Show a Custom Message, at Every Clone/Fetch?

How to disable deleting folders in root but allow subfolders/files to be created/modified?

Vue.js redirection to another page

How to remove X-CMAE-Envelope from php mail

Translate function

Django Test Case Error 'WSGIRequest' object has no attribute 'session'

Issue Installing Elastix 4.0 via Bootable USB - CentOS7 Error

React-native: scrollview inside of panResponder

How to dynamically extract data from dropdown lists or multiple textboxes using import.io

How to add image and keep text on PivotItem

Importing source files and folders into IAR Workbench

Top