Hey guys, well my question is i've seen examples of classes being to create like a function. But for example i saw someone made for a socket it was something along the lines of this: pySocket <host> <port> but i'm confused on how to create something like that. To get me started, would you start off with a class or an object?

Recommended Answers

All 2 Replies

It sounds like you have your terms a little confused...

Actually, after reading that again... I tihnk you have me a little confused...

Maybe we will be able to help you better if you give us a little more information.

your pysocket example sounds like it is a script that takes command line args...

socket is a standard class in the socket module. You should use it if you want to create a socket. However it often happens that one writes functions to create new objects. These functions call the normal object constructors. For example if you want to create a new socket connected to a given address, and also a new list with a specific content, you could define functions like this

def my_socket(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect('', port)
   return sock

def my_list(n):
    return range(n)

In python code, such functions are similar to classes in that they create new objects when they are called: there is no class my_list for example.
It's also possible to write functions which return new classes, but it's a different matter. Here is such a function

def my_new_list_class(name):
    the_class =  type(name, (list, ), {})
    return the_class

This one will return a new class each time it's called !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.