6

Let me start with a disclaimer: I'm not the best programmer out there. I do however study I.T. and learnt a bit of Java and C.

I'm getting stuck into Python and Django + Mongoengine, I'm not going to explain it into detail as it will deviate from the original question.

While trying to understand what is happening at this bit of Django code I was shocked by some of concepts I seemed to understand.

Mostly I was completely confused by how one object could add accessible attributes to a parent object and how Python defines __set__() and __get__() in such a way as to be so confusing as that.

Could someone tell me how orthodox this design style is and whether I find it bizarre because I'm an amateur programmer.

Martijn Pieters
  • 14,499
  • 10
  • 57
  • 58
holografix
  • 163
  • 3
  • Hi Martijn, what I mean is the lack of obvious object initialization, then the object's set() and get() are used in a very strange manner, basically set() changes what the owner of the object returns! – holografix Apr 14 '13 at 13:21
  • Hey Gnat, thanks for the comment. Perhaps my question should mention: How Pythonic does that bit of code look to you guys? To me it's **incredibly** unreadable / hard to follow. – holografix Apr 14 '13 at 13:27
  • 2
    Could you describe what you find odd? There are various things going on there, and your description "in such a way" is rather vauge. – Winston Ewert Apr 14 '13 at 13:42

1 Answers1

7

You are looking at a descriptor. Descriptors are special types of objects that can alter their behaviour when bound to a Python class or instance. They give you a hook into class and instance attribute interactions (setting, getting and deleting).

Python functions and the property decorator are descriptors. Functions have a __get__ method that returns a method wrapper when accessed; this is how Python methods can pass in the self parameter when you call a method on an instance. property objects have __get__, __set__ and __del__ methods to handle attribute interactions and translate those interactions into method calls.

The FileDescriptor class you linked to, translates attribute access into a lookup for the named field on the instance they are bound to, guaranteeing that a FieldFile object is returned. It acts as a proxy for the field, and it is therefor completely natural that they change the named field on the instance they are bound to.

See the Descriptor HOWTO Guide and the Implementing Descriptors section of the Python Datamodel documentation for more details.

Martijn Pieters
  • 14,499
  • 10
  • 57
  • 58
  • Martijn that's a fantastic answer thanks so much. You know, this concept of descriptors is exactly the kind of thing I reckon separates amateurs, from proper Software Engineers. Could I also bother you to help me with this: [Stack Overflow Question](http://stackoverflow.com/questions/15862912/mongoengine-filefield-saving-to-disk) – holografix Apr 15 '13 at 01:37
  • I am not familiar with Mongo at all, so not sure I can be of help there. – Martijn Pieters Apr 15 '13 at 09:49