Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:24:13+00:00 2026-05-15T14:24:13+00:00

I have a simple socket class: MySocketLib.py .. import socket class socklib(): def create_tcp_serversocket(self,port):

  • 0

I have a simple socket class: MySocketLib.py ..

import socket

class socklib():
    def create_tcp_serversocket(self,port):
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serversocket.bind((socket.gethostname(), port))
        serversocket.listen(5) 
        return serversocket

    def send_msg(self, sock, msg, msglen):
         ... blah blah ...  

    def recv_msg(self, sock, msglen):
         ... blah blah .... 

My main file is Server.py as follows:

import MySocketLib

class serverclass(): 

    def __init__(self,port):
        self.servsock = 0
        self.port = port

    def run(self):
        self.servsock = self.create_tcp_serversocket(self.port)
        (clientsock, address) = self.servsock.accept() # ERROR: 'SELF' not defined
        ... blah blah ... 

#############################
###   Main startup code

if __name__ == '__main__':
    server = serverclass(2000) # server is on port 2000
    server.run()

I keep getting the below error:

File "D:\CodeLearn\Server.py", line 14, in serverclass
(clientsock, address) = self.servsock.accept()
NameError: name 'self' is not defined

I can’t seem to get a handle on the concept of “self”. Some times, it seems to be needed and sometimes not. Why would i need to define ‘self’ here when it is a python keyword?


Thanks everyone … it was indeed the tabs and spacing issues. I use eclipse and in preferences I have set “Editor” preferences to use spaces when tabbing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:24:13+00:00Added an answer on May 15, 2026 at 2:24 pm

    Don’t use tabs in Python source code. Configure your editor to always use spaces.

    self is not a Python keyword, it’s a convention. It’s the usual name for the “instance” of a class you’re using. Example:

    class X:
        def __init__(self, v): self.v = v
    
    a = X(1)
    b = X(2)
    print a.v, b.v
    

    When this code runs, the Python runtime will eventually allocate memory for two instances of X which it will assign to a and b respectively. If there wasn’t something like self, you would have to write:

    a = X()
    a.v = 1
    b = X()
    b.b = 2
    print a.v, b.v
    

    And you would get an error because you wrote b.b instead of b.v. Moreover, calling methods would be outright ugly:

    class X:
        def set(v): ???.v = v
    

    How would you say “access the reference v which was allocated in __init__ of X“?

    One solution would be to pass the instance reference (the pointer to the memory which was allocated for X) as a parameter:

    class X:
        def set(a, v): a.v = v
    
    a.set(a, 1) # Holy ugly!
    

    Everyone would use different names and we would all violate DRY and it would be a mess. So what Python did is:

    class X:
        def set(self, v): self.v = v
    
    a.set(1) # while "set" runs, "self" == "a"
    b.set(2) # while "set" runs, "self" == "b"
    

    That said, I have no idea why the code above fails. self is obviously defined or the error would already happen in the first line of your run() method. So my guess is that you mixed spaces and tabs for indentation and that confuses Python and it sees:

    def run(self):
        self.servsock = self.create_tcp_serversocket(self.port)
    (clientsock, address) = self.servsock.accept() # ERROR: 'SELF' not defined
    

    Then it would try to evaluate self.servsock.accept() while it parses the class definition. At that time, there is no instance yet (the class doesn’t exist!) so self is not available either.

    Conclusion: Never mix tabs and spaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've got this code in my socket class: bool GSocket::Listen(int Port) { d->Socket =
I am thinking to write a simple wrapper class for socket in C++. I
Let's say I have a simple Server with a template which accepts a Client
I have a simple server that waits for a client to connect, reads the
I have a very simple server/client performance test using boost::asio on Windows and it
I have a fairly simple game that works perfectly on every version now up
I have simple 3 tier web application and have mostly CRUDE functionalities. Recently I
I have simple one column HTML files ( ebooks from Gutenberg Project). I want
I have a simple HelloWorld Activity that I try to test with an Android
I currently work on a server/client program based on asynchronous socket. As far as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.