Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting Django Source Code

I was looking through some of the Django source code and came across this. What exactly does: encoding = property(lambda self: self.file.encoding) do?

like image 337
HighLife Avatar asked May 16 '12 06:05

HighLife


3 Answers

There's nothing wrong with the other two answers, but they might be a little high-level. So here's the 101 version:

lambda

Although it's in their documentation for C#, I think Microsoft actually has the best explanation of the concept of lambda:

A lambda expression is an anonymous function that can contain expressions and statements

Most people without an official CS degree trip over lambda, but when you think of it as simply an "anonymous function", I think it becomes much easier to understand. The format for lambda in Python is:

lambda [argument]: [expression]

Where [argument] can be nothing, a single argument or a comma-delimited list of arguments and [expression] is essentially the method body. That's why @Jordan said the code you mentioned is roughly the equivalent of:

def encoding(self):
    return self.file.encoding

self is the argument passed into the method and the return value of the method (self.file.encoding) is the expression.

property

The property method allows you to create "getters" and "setters", basically, for an attribute on a class. In traditional OOP, "members", or the attributes of a class, are usually set as protected or private -- you never actually access the attribute directly. Instead, you access methods that in turn retrieve or manipulate the attribute. Chief among those would get the getter and the setter. As their names pretty much describe, they are methods that get and set the value of an attribute, respectively.

Now, Python OOP doesn't really have a concept of protected or private attributes in the truest sense. You are free to follow the rules, but there's nothing stopping you from accessing anything you want on a class. So, getters and setters are most normally, in Python, used in conjunction with property to "fake" an attribute, for lack of a better word. For example:

def get_foo(self):
    return self.bar

def set_foo(self, value):
    self.bar = value

foo = property(get_foo, set_foo)

With that I can now do things like instance.foo (no parenthesis) and instance.foo = 'something'. And it works just as if foo was a regular attribute on the class.

In the code you mention, they're only setting a getter, but it works the same. encoding will act like an attribute on the class and returns the value of file.encoding.

like image 149
Chris Pratt Avatar answered Oct 23 '22 06:10

Chris Pratt


It's basically shorthand for a fullblown wrapped getter. Expanded would look something like this, although it's not a true 1-1 expansion.

def encoding(self):
    return self.file.encoding
like image 34
Jordan Avatar answered Oct 23 '22 04:10

Jordan


It is a property that proxies access from the containing class to it's file.encoding attribute.

like image 36
XORcist Avatar answered Oct 23 '22 05:10

XORcist