python导入类属性不存在,Python类找不到属性

Basically, I want to run the connect function but I keep getting the CMD error message 'class StraussBot has no attribute 'connectSock' but I can obviously see it does. I've tried searching on here and I can't find any resolutions to this issue. SO it will be greatly appreciated if you could help me find why this isn't finding the 'connectSock' function.

Code:

import socket

from config import HOST, PORT, CHANNEL

# User Info

USER = "straussbot" # The bots username

PASS = "oauth:sj175lp884ji5c9las089sm9vvaklf" # The auth code

class StraussBot:

def __init__(self):

self.Ssock = socket.socket()

def connectSock(self):

self.Ssock.connect((HOST, PORT))

self.Ssock.send(str("Pass " + PASS + "\r\n").encode('UTF-8'))

self.Ssock.send(str("NICK " + USER + "\r\n").encode('UTF-8'))

self.Ssock.send(str("JOIN " + CHANNEL + "\r\n").encode('UTF-8'))

if __name__ == "__main__":

print "Starting the bot..."

while True:

straussbot = StraussBot

try:

straussbot.connectSock()

except Exception as e:

print e

解决方案

You are getting confused by the error here. You get an AttributeError for self.Ssock because you do not have an instance.

You only created a reference to the class here:

straussbot = StraussBot

You need to call the class to produce an instance:

straussbot = StraussBot()

You are also mixing tabs and spaces:

gJ7lx.png

Note how lines 5 through 9 have lines in the indentation, but the rest have dots? Those are tabs, and Python sees those as 8 spaces. So your connectSock method is indented inside of __init__ and not seen as a method on StrausBot.

You'll have to stick to either just tabs or just spaces. Python's styleguide strongly recommends you use spaces only.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值