0
class Socket:
    def __init__(self, ip, port, auto_connect=False):
        self.ip = ip
        self.port = port
        self.sockfd = socket.socket()
        if auto_connect:
           self.connect()

    @staticmethod
    def connect(sockfd, ip, port):
        sockfd.connect((ip, port))

    @nonstaticmethod
    def connect(self):
        self.sockfd.connect((self.ip, self.port))

Similar to how other languages like Dlang do it where you can define functions that have the same names; but different signatures, thus allowing the compiler to distinguish what function ambiguous function calls refer to.

I had the thought that you could somehow imitate properties, because they allow for you to enact on the same function but use different inputs and receive different outputs. But since the @property tag isn't Python, and recreating it in C isn't something very feasible nor efficient work-wise; I'm seeking a way to do it either purely or with 3rd-party libraries.

class Socket:
    def __init__(self, ip, port, auto_connect=False):
        self.ip = ip
        self.port = port
        self.sockfd = socket.socket()
        if auto_connect:
           self.connect()

    @staticmethod
    def static_connect(sockfd, ip, port):
        sockfd.connect((ip, port))

    def connect(self):
        self.sockfd.connect((self.ip, self.port))

I did as well figure that you could just name the functions differently but I just don't like this approach since it involves the user knowing that the secondary function exists and although sensible, not my style.

Also I did suggest to myself the fact that I could do something along the lines of this:

class Socket: ... def connect(self, ip, port): """ When using statically: self = socket ip = ip port = port When using as a class member: ip = ip & port = port """

    if isinstance(self, types.ClassType):
        self.sockfd.connect((ip, port))
        return
    self.connect((ip, port))

But that's just unnatural and could cause confusion when somebody reads the code, and is over-all unPythonic.

NOTE: This function replicates behaviour shown in here but it luckily manages to utilize all the parameters and thus no optional parameters are required, so apologies for not mentioning it explicitly, but my question doesn't ask for a way to do it with key word arguments.

tl;dr:

class functions that have different signatures e.g.: static method but don't replace each other- and yet still can be called appropriately

uspectaculum
  • 391
  • 2
  • 9
  • Possible duplicate of [How do I use method overloading in Python?](https://stackoverflow.com/questions/10202938/how-do-i-use-method-overloading-in-python) – Christian Dean Dec 16 '17 at 23:54

1 Answers1

0

How about a method with default keyword arguments ?

class Socket:
    def __init__(self, ip, port, auto_connect=False):
        self.ip = ip
        self.port = port
        self.sockfd = socket.socket()
        if auto_connect:
           self.connect()

    def connect(sockfd=self.sockfd, ip=self.ip, port=self.port):
        sockfd.connect((ip, port))

passed arguments will override defaults, but otherwise when called without arguments will use its default parameters.

Is that a solution that can work for you ?

Evya
  • 2,325
  • 3
  • 11
  • 22
  • I talked about the method later on in my post; however it's not what I'd like to do, I'd like to replicate the behaviour of something like `@property`, but I'm starting to think that it's the only proper way to do it without diving into internals. – uspectaculum Dec 16 '17 at 23:58
  • 1
    I'm pretty sure this is the proper way of implementing method overloading in Python (excluding 3rd party libraries, but that's just going over the top imo). – Evya Dec 17 '17 at 00:09