, `with` python, __repr__ (print)?

, with

Python. , __enter__

__exit__

, , . , :

import tempfile, os, shutil
class temp_workspace(object):

    def __enter__(self):
        self.local_dir = os.getcwd()
        self.temp_dir  = tempfile.mkdtemp()
        os.chdir(self.temp_dir)

    def __exit__(self, exc_type, exc_value, traceback):
        os.chdir(self.local_dir)
        shutil.rmtree(self.temp_dir)

    def __repr__(self):
        return self.temp_dir

      

, :

with temp_workspace() as T:
    print "Temp directory name is ", T

      

None

__repr__

! , T

NoneType

. ?

+3




1


__enter__

, . return self

__enter__

.



+5









All Articles