M
M
Myhabr022020-08-20 19:44:53
Python
Myhabr02, 2020-08-20 19:44:53

Why does it lose self when importing a class in python?

I create a class, in the function.py file

class fnc():
  def send_msg(self, message = None,attachment = None,**args):
    vk.messages.send(
      peer_id = peer_id,
      random_id = get_random_id(),
      disable_mentions = 1,
      message = message,
      attachment = attachment, **args)

I import and call it in the bot.py file
import vk_api
import requests
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api.bot_longpoll import VkBotEventType
from vk_api.utils import get_random_id
from function import fnc

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
      user_stat_id = event.obj['message']['from_id']
      vk_message = event.obj['message']
      peer_id = vk_message['peer_id']
      text = vk_message['text']
      if  text.lower() == '1':
        fnc.send_msg(message="text")

gives an error:
File "bot.py", line 34, in
fnc.send_msg(message="text")
TypeError: send_msg() missing 1 required positional argument: 'self
'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web Dentist, 2020-08-20
@Myhabr02

You need to instantiate the class.
fnc = fnc()
And in general it would be better
class Fnc:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question