63012

missing 1 required positional argument:'self' when assigning a method to a variable

Question:

I am trying to assign a method's return to a variable and stuck with this error.

class MyClass(): def my_def(self): return "Hello" my_variable = my_def()

Here is the Java equivalent of what I want to do.

public class NewException { public int method1(){ return 1; } public int variable = method1(); }

I am sure this is something simple, but I couldn't even find the right words to google this. Any help is appreciated.

Answer1:

Lets start with the difference between methods and functions, basically a method belongs to some object while a function does not. So for example

def myFunction(): return "F" class MyClass: value = 0 def myMethod(self, value): old = self.value self.value = value return old myClassInstance = MyClass() print myClassInstance.myMethod(3) # 0 print myClassInstance.myMethod(33) # 3 print myFunction() # F

Notice that the method is bound to the instance and it doesn't make sense to call the method before the instance is created. With that in mind, your error should make more sense. The method cannot be called without an instance (self). This is not the only kind of method, for example there are "static methods". Static methods are defined on the class, but they are called without an instance. For example:

class MyClass: @staticmethod def myStaticMethod(): return "static method" # Consider using an instance attribute instead of a class attribute def __init__(self): self.instance_attribute = MyClass.myStaticMethod() # Or if you need a class attribute it needs to go outside the class block MyClass.class_attribute = MyClass.myStaticMethod()

Recommend

  • list/vector of class methods in a class variable
  • In PHP, are objects methods code duplicated or shared between instances?
  • How to get scenario name from a scenario outline in cucumber using java
  • Enterprise Architect - Cancel Connectors Bridges
  • TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
  • Use preg_match to find if string contains script-tags
  • Distribute Range of Numbers between each threads
  • How to remove the dot in to_char if the number is an integer
  • How to send control C to Mac Terminal using python?
  • Add spaces between words in spaceless string
  • How can I print out a large table of symbols from MATLAB to form a cross-stitch chart?
  • help('modules') crashing? Not sure how to fix
  • Ruby and class variables in inherit class
  • Can XOR be expressed using SKI combinators?
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Most efficient way to move table rows from one table to another
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • How to delay loading a property with linq to sql external mapping?
  • Seeking advice on Jetty HttpClient Hang
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Cannot Parse HTML Data Using Android / JSOUP
  • PHP: When would you need the self:: keyword?
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Does armcc optimizes non-volatile variables with -O0?
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • Android Heatmap on canvas or ImageView
  • java string with new operator and a literal