In general, objects do not know which variables are assigned to them:
tmp = namedtuple('OrderedPair', ['x','y'])
Point = tmp
Coordinate = tmp
This is a problem for named tuples. We must pass the class name to the namedtuple () factory function so that the class can get the usable name, docstring and __repr__, all of which have the class name inside it.
, -. Python def class, , ( docstring) .
, def:
def square(x):
'Return a value times itself'
return x * x
def ( , "" ):
tmp = lambda x: x*x # create a function object
tmp.__name__ = 'square' # assign its metadata
tmp.__doc__ = 'Return a value times itself'
square = tmp # assign the function to a variable
. class , :
class Dog(object):
def bark(self):
return 'Woof!'
( , "" ):
Dog = type('Dog', (object,), {'bark': lambda self: 'Woof'})
, def class, . . , Python, def class :
survey_results = open('survey_results')
company_db = sqlite3.connect('company.db')
www_python_org = urllib.urlopen('http://www.python.org')
radius = property(radius)
, . PEP 359, make, def, class import.
make <callable> <name> <tuple>:
<block>
:
<name> = <callable>("<name>", <tuple>, <namespace>)
, Guido "", , ( , ).
, , . . , . , : -)