Skip to main content
gamedev.net
🔒 Locked

python argument paradigm

Started by thedustbustr 5 replies 1k views
Original Post
thedustbustr
thedustbustr
class Packet(object): blah blah blah 

def encode(payload):
    if type(payload) is not type(Packet()):
        raise MyException("LayerThree.encode(payload): payload must be a packet")
    now do some stuff
this can't possibly be the Pythonic solution to 'typifying' arguments. [Edited by - thedustbustr on February 19, 2007 10:20:05 PM]
thedustbustr
thedustbustr
move to for beginners or whatever, sorry
Sneftel
Sneftel
I think the normal solution would be to define "encode" as a member function of Packet. Of course, depending on what exactly encode should do, you might want pickle or marshal.
thedustbustr
thedustbustr
well, in different network models, there are several different layers, each wrapping the output of the previous, so making it a member isn't very intuitive a sol'n to me.

thanks for the marshal link, that will be helpful later on.
Sneftel
Sneftel
Quote:
Original post by thedustbustr
well, in different network models, there are several different layers, each wrapping the output of the previous, so making it a member isn't very intuitive a sol'n to me.

But past the first layer, does the object type matter? It seems like "encode" would be called to take care of serialization, and then the lower layers of the network model would be handled separately. Or am I mistaking your question? You've asked whether a particular code snippet is the solution to "this problem", but it's not clear what "this problem" is.
thedustbustr
thedustbustr
my function name is probably misleading.

disclaimer: this is homework. I am simulating a network protocol. The application layer creates a payload, which is passed to layer two, which gives it a header and footer. This wrapped packet is passed to layer three, which gives it another header and footer. blah blah blah.

The prof isn't very clear about future requirements, so I havent really settled into a design, I just made it work and handed it in.

so my encode function (its really a method in my layerthree class) accepts a payload, wraps it, and returns the new packet. but the payload needs to be of type Packet because my packet class is a specialized deque (this could maybe eventually be switched over to a byte array or other binary type but for now that's impossible due to the requirement that a packet can be any number of bits, not just multiples of 8). layer two has a similar method, and also calls Packet methods.

one sol'n would be to create a new packet with the old packet sandwiched by new data, but that seemed silly when I can reuse it.

so, i'm currently using a much worse (read: horrible) sol'n of checking the type() of the payload manually which is dumb but easy, and was hoping there was an elegant and easy solution.
Vorpy
Vorpy
The Pythonic solution is to remove the type check. If the code tries accessing an attribute that doesn't exist, it will throw an exception anyway(AttributeError or something like that). Besides, are you actually planning on passing the wrong type of argument to this function?

Basically in Python the default is to avoid type checks. If you decide to replace Packet with a different implementation, all you need to do is make sure it conforms to the same interface and all your code that was written for Packets will still work.

The python standard libraries are (mostly) written like this with respect to files. Everything made to work with files is only expecting a file-like object. Things that only read files only need the read methods of file. Things that only write files only need the write methods. Data in memory or over the network or in a database can be wrapped in a file-like object to allow it to be used by anything expecting a file.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.