Skip to main content
deleted 12 characters in body
Source Link

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it.

As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some inheritance code.

Class A
  def foo
    print "foo"

Class B(A)
  def bar
    print "bar"

c = new B()
c.foo()
>>> prints: foo

okay, to me that seems like very normal basic inheritance (please let me know if you disagree). So to get back to my chatserver, I thought it would be nice to extend the socket and make it into a chat socket. The first thing I wanted to do is add an attribute called nickname (important in a chat). Oh, and I thought about using select as a threading solution because it seemed to come highly recommended, so you need an array of sockets (I also like to hear your thoughts on this). Now, the problem you get is, that socket, instantiates sockets, so

s = socket.socket(family, type,..)

in stead of

s = new Socket()

so what I wanted todo is

class ChatSocket(Socket)
  def getNickname()
    return "bob" # obviously an example for simplicity

s = new ChatSocket() 

First question: Why does it work this way and doesn't it have a 'normal' instantiation?

Second question: What is in your opinion an elegant solution to this challenge?

I hope this makes sense... Any thoughts are very welcome

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it.

As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some inheritance code.

Class A
  def foo
    print "foo"

Class B(A)
  def bar
    print "bar"

c = new B()
c.foo()
>>> prints: foo

okay, to me that seems like very normal basic inheritance (please let me know if you disagree). So to get back to my chatserver, I thought it would be nice to extend the socket and make it into a chat socket. The first thing I wanted to do is add an attribute called nickname (important in a chat). Oh, and I thought about using select as a threading solution because it seemed to come highly recommended, so you need an array of sockets (I also like to hear your thoughts on this). Now, the problem you get is, that socket, instantiates sockets, so

s = socket.socket(family, type,..)

in stead of

s = new Socket()

so what I wanted todo is

class ChatSocket(Socket)
  def getNickname()
    return "bob" # obviously an example for simplicity

s = new ChatSocket() 

First question: Why does it work this way and doesn't it have a 'normal' instantiation?

Second question: What is in your opinion an elegant solution to this challenge?

I hope this makes sense... Any thoughts are very welcome

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it.

As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some inheritance code.

Class A
  def foo
    print "foo"

Class B(A)
  def bar
    print "bar"

c = B()
c.foo()
>>> prints: foo

okay, to me that seems like very normal basic inheritance (please let me know if you disagree). So to get back to my chatserver, I thought it would be nice to extend the socket and make it into a chat socket. The first thing I wanted to do is add an attribute called nickname (important in a chat). Oh, and I thought about using select as a threading solution because it seemed to come highly recommended, so you need an array of sockets (I also like to hear your thoughts on this). Now, the problem you get is, that socket, instantiates sockets, so

s = socket.socket(family, type,..)

in stead of

s = Socket()

so what I wanted todo is

class ChatSocket(Socket)
  def getNickname()
    return "bob" # obviously an example for simplicity

s = ChatSocket() 

First question: Why does it work this way and doesn't it have a 'normal' instantiation?

Second question: What is in your opinion an elegant solution to this challenge?

I hope this makes sense... Any thoughts are very welcome

Source Link

Python OO problem

I started learning Python yesterday and I ran into a problem. I like to hear some thoughts on it.

As an exercise, I decided to build a chatserver. As part of the exercise, I wanted to write some inheritance code.

Class A
  def foo
    print "foo"

Class B(A)
  def bar
    print "bar"

c = new B()
c.foo()
>>> prints: foo

okay, to me that seems like very normal basic inheritance (please let me know if you disagree). So to get back to my chatserver, I thought it would be nice to extend the socket and make it into a chat socket. The first thing I wanted to do is add an attribute called nickname (important in a chat). Oh, and I thought about using select as a threading solution because it seemed to come highly recommended, so you need an array of sockets (I also like to hear your thoughts on this). Now, the problem you get is, that socket, instantiates sockets, so

s = socket.socket(family, type,..)

in stead of

s = new Socket()

so what I wanted todo is

class ChatSocket(Socket)
  def getNickname()
    return "bob" # obviously an example for simplicity

s = new ChatSocket() 

First question: Why does it work this way and doesn't it have a 'normal' instantiation?

Second question: What is in your opinion an elegant solution to this challenge?

I hope this makes sense... Any thoughts are very welcome