Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
positional argument: 'self'
#1
I write the following code and get the following error.
import random

word_list = ['a', 'ranom', 'list', 'of', 'words']

class Word:
    
    def get_word(self):
        words = random.choice(word_list)
        return words


w = Word.get_word()
print(w)
Error:
Traceback (most recent call last): File "c:\Users\mcmxl\mystuff\hangman\test_hangman.py", line 13, in <module> w = Word.get_word() TypeError: get_word() missing 1 required positional argument: 'self'
If I change line 13 to w = Word.get_word('') everything works fine. I know there is a way to make it so I don't have to
use the empty string as the self argument. Could someone explain it to me?
Reply
#2
remove self and it will work as expected
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Dec-12-2021, 08:08 PM)menator01 Wrote: remove self and it will work as expected
I'm trying to learn how to use classes and functions with the "self" argument. Removing it doesn't teach me anything.
Reply
#4
Create an instance of Word
word = Word()
w = word.get_word()

print(w)
Reply
#5
A link on self
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

Another example

#! /usr/bin/env python3

import random as rnd

word_list = ['a', 'random', 'list', 'of', 'words']

class Word:
    def __init__(self):
        self.my_list = None

    def get_word(self):
        word = rnd.choice(self.my_list)
        return word

word = Word() # Initiate class instant
word.my_list = word_list # Assign word list to instance variable
print(word.get_word()) # Print results of get word
print(word.get_word()) # Print another result
Output:
words list
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
A more normal and standard way to do this menator01.
import random

class Word:
    def __init__(self, word_list):
        self.word_list = word_list

    def get_word(self):
        words = random.choice(self.word_list)
        return words

word_list = ['a', 'ranom', 'list', 'of', 'words']
word = Word(word_list)
for w in range(5):
    print(word.get_word())
Output:
words of of ranom words
Reply
#7
(Dec-12-2021, 08:54 PM)snippsat Wrote: A more normal and standard way to do this menator01.
import random

class Word:
    def __init__(self, word_list):
        self.word_list = word_list

    def get_word(self):
        words = random.choice(self.word_list)
        return words

word_list = ['a', 'ranom', 'list', 'of', 'words']
word = Word(word_list)
for w in range(5):
    print(word.get_word())
Output:
words of of ranom words

This is what I need to study and wrap my head around.
Reply
#8
(Dec-12-2021, 08:54 PM)snippsat Wrote: A more normal and standard way to do this menator01.
import random

class Word:
    def __init__(self, word_list):
        self.word_list = word_list

    def get_word(self):
        words = random.choice(self.word_list)
        return words

word_list = ['a', 'ranom', 'list', 'of', 'words']
word = Word(word_list)
for w in range(5):
    print(word.get_word())
Output:
words of of ranom words

True. My example was to initiate the class without setting the instance variable right away.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
There are three kinds of methods; instance, class and static.
class DemoClass:
    def instance_method(*args):
        print("Instance method", args)

    @classmethod
    def class_method(*args):
        print("Class method", args)

    @staticmethod
    def static_method(*args):
        print("Static method", args)

instance = DemoClass()

instance.instance_method('Hello')
instance.class_method('Hello')
instance.static_method('Hello')

DemoClass.class_method('Hello')
DemoClass.static_method('Hello')
Output:
Instance method (<__main__.DemoClass object at 0x0000027A7F634F10>, 'Hello') Class method (<class '__main__.DemoClass'>, 'Hello') Static method ('Hello',) Class method (<class '__main__.DemoClass'>, 'Hello') Static method ('Hello',)
When calling an instance method, the instance is passed as the first argument and 'Hello' appears as the second argument. Essentially Python converts this:
instance.instance_method('Hello')
to this:
getattr(instance.__class__, "instance_method")(instance, 'Hello')
A class method is identified by the "@classmethod" decorator. A class method cannot access any instance variables because the class method is passed a class reference as the first argument instead of an instance of the class. Essentially the class method decorator converts these:
instance.class_method('Hello')
DemoClass.class_method('Hello')
To these:
getattr(instance.__class__, "class_method")(instance.__class__, 'Hello')
getattr(DemoClass, "class_method")(DemoClass, 'Hello')
A static method is a function that resides in the class namespace. Unlike instance methods and class methods, no arguments are added when calling the method. The first argument in the method call is the first argument in the method argument list.

In the original post, the get_word() method references an instance variable, so it has to be an instance method, and must be called using an instance. The instance becomes the first argument to the method and the method must have an instance argument as the fist argument to the method.
def get_word(self):
    return random.choice(self.word_list)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,847 May-09-2022, 12:49 PM
Last Post: deanhystad
  What is positional argument self? Frankduc 22 5,751 Mar-06-2022, 01:18 AM
Last Post: Frankduc
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,977 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 5,486 Jan-06-2021, 04:25 PM
Last Post: Insen
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 12,321 Jun-17-2020, 07:25 PM
Last Post: sveto4ka
  missing 1 required positional argument: 'self' yasser 7 11,510 Jun-07-2020, 06:48 AM
Last Post: ndc85430
  TypeError: _linspace_dispatcher() missing 1 required positional argument: 'stop' Ae_Lovecraft 3 8,589 May-28-2020, 03:33 PM
Last Post: Larz60+
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,851 Mar-03-2020, 08:34 AM
Last Post: buran
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 21,959 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  missing 1 required positional argument: psosmol 7 13,201 Apr-16-2019, 10:07 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020