Python “Object has no attribute”, but actually it has

I’am preparing two scripts: one with class to communicate with DC Power Supply, and second to manage the communication.

dcom.py:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import socket
class supply:
def __init__(self, IP, PORT, BUFFER_SIZE, TIMEOUT_SECONDS):
self.IP = IP
self.PORT = PORT
self.BUFFER_SIZE = BUFFER_SIZE
self.TIMEOUT_SECONDS = TIMEOUT_SECONDS
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def opensocket(self):
supply.connection.connect((supply.IP, supply.PORT))
supply.connection.settimeout(supply.TIMEOUT_SECONDS)
def sendAndReceiveCommand(self, msg):
msg = msg + 'n'
supply.connection.sendall(msg.encode('UTF-8'))
return supply.connection.recv(supply.BUFFER_SIZE).decode('UTF-8').rstrip()
def sendCommand(self, msg):
msg = msg + 'n'
supply.connection.sendall(msg.encode('UTF-8'))
def initialize(self):
return supply.sendAndReceiveCommand('*IDN?')
def readVoltage(self):
return supply.sendAndReceiveCommand('SOUR:VOLT?')
def readCurrent(self):
return supply.sendAndReceiveCommand('SOUR:CUR?')
</code>
<code>import socket class supply: def __init__(self, IP, PORT, BUFFER_SIZE, TIMEOUT_SECONDS): self.IP = IP self.PORT = PORT self.BUFFER_SIZE = BUFFER_SIZE self.TIMEOUT_SECONDS = TIMEOUT_SECONDS self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def opensocket(self): supply.connection.connect((supply.IP, supply.PORT)) supply.connection.settimeout(supply.TIMEOUT_SECONDS) def sendAndReceiveCommand(self, msg): msg = msg + 'n' supply.connection.sendall(msg.encode('UTF-8')) return supply.connection.recv(supply.BUFFER_SIZE).decode('UTF-8').rstrip() def sendCommand(self, msg): msg = msg + 'n' supply.connection.sendall(msg.encode('UTF-8')) def initialize(self): return supply.sendAndReceiveCommand('*IDN?') def readVoltage(self): return supply.sendAndReceiveCommand('SOUR:VOLT?') def readCurrent(self): return supply.sendAndReceiveCommand('SOUR:CUR?') </code>
import socket

class supply:

    def __init__(self, IP, PORT, BUFFER_SIZE, TIMEOUT_SECONDS):
        self.IP = IP
        self.PORT = PORT
        self.BUFFER_SIZE = BUFFER_SIZE
        self.TIMEOUT_SECONDS = TIMEOUT_SECONDS
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def opensocket(self):
        supply.connection.connect((supply.IP, supply.PORT))
        supply.connection.settimeout(supply.TIMEOUT_SECONDS)

    def sendAndReceiveCommand(self, msg):
        msg = msg + 'n'
        supply.connection.sendall(msg.encode('UTF-8'))
        return supply.connection.recv(supply.BUFFER_SIZE).decode('UTF-8').rstrip()
    
    def sendCommand(self, msg):
        msg =  msg + 'n'
        supply.connection.sendall(msg.encode('UTF-8'))

    def initialize(self):
        return supply.sendAndReceiveCommand('*IDN?')

    def readVoltage(self):
        return supply.sendAndReceiveCommand('SOUR:VOLT?')

    def readCurrent(self):
        return supply.sendAndReceiveCommand('SOUR:CUR?')

main_comm.py:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import dcom
SUPPLY_IP = '10.41.32.2'
SUPPLY_PORT = 8462
BUFFER_SIZE = 128
TIMEOUT_SECONDS = 10
supply = dcom.supply(SUPPLY_IP, SUPPLY_PORT, BUFFER_SIZE, TIMEOUT_SECONDS)
print(supply.connection)
supply.opensocket()
</code>
<code>import dcom SUPPLY_IP = '10.41.32.2' SUPPLY_PORT = 8462 BUFFER_SIZE = 128 TIMEOUT_SECONDS = 10 supply = dcom.supply(SUPPLY_IP, SUPPLY_PORT, BUFFER_SIZE, TIMEOUT_SECONDS) print(supply.connection) supply.opensocket() </code>
import dcom

SUPPLY_IP = '10.41.32.2'
SUPPLY_PORT = 8462
BUFFER_SIZE = 128
TIMEOUT_SECONDS = 10

supply = dcom.supply(SUPPLY_IP, SUPPLY_PORT, BUFFER_SIZE, TIMEOUT_SECONDS)

print(supply.connection)
supply.opensocket()

When I run the main_comm.py the result is:

<socket.socket fd=400, family=2, type=1, proto=0>
Traceback (most recent call last):
File “C:UsersRyszardDocumentsWroblewskiWDC_supply_commmain_comm.py”, line 11, in <module>
supply.opensocket()
File “C:UsersRyszardDocumentsWroblewskiWDC_supply_commdcom.py”, line 15, in opensocket
supply.connection.connect((supply.IP, supply.PORT))
^^^^^^^^^^^^^^^^^
AttributeError: type object ‘supply’ has no attribute ‘connection’

As you can see, first I can print the object attribute “connection” value, what I suppose means that this attribute exists. However, later I get an error that this object has no such attribute.
Do you have any idea how to fix it?

New contributor

Didik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật