Django source code interpretation

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

+5
source share
3 answers

There is nothing wrong with the other two answers, but they can be a bit high. So here is version 101:

lambda

Although this is in their C # documentation, I think Microsoft really has a better explanation of the concept of lambda:

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

, CS, , " ", , . Python:

lambda [argument]: [expression]

[argument] , - [expression] - , , . @Jordan , , , :

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

self - , , (self.file.encoding) .

property "" "", , . "" - . , , , . . , , , .

Python OOP . , -, . , Python property "" - . :

def get_foo(self):
    return self.bar

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

foo = property(get_foo, set_foo)

, instance.foo ( ) instance.foo = 'something'. , foo .

, , , . encoding file.encoding.

+4

. Expanded , 1-1.

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

This is a property that proxies access from the containing class to this file.encoding attribute .

+2
source

All Articles