関数をクラスに配置する必要があるという印象を受けているようです。
これはそうではありません。shutil
はクラスではなく、モジュールです。モジュールが定義する唯一のクラスshutil
は例外です。文書化された API の他のすべてはトップレベル関数です。shutil
ソースコードを見ることができます。move
関数は、モジュールのソース コードで次のように直接定義されます。
def move(src, dst):
"""Recursively move a file or directory to another location. This is
similar to the Unix "mv" command.
If the destination is a directory or a symlink to a directory, the source
is moved inside the directory. The destination path must not already
exist.
If the destination already exists but is not a directory, it may be
overwritten depending on os.rename() semantics.
If the destination is on our current filesystem, then rename() is used.
Otherwise, src is copied to the destination and then removed.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
"""
real_dst = dst
if os.path.isdir(dst):
if _samefile(src, dst):
# We might be on a case insensitive filesystem,
# perform the rename anyway.
os.rename(src, dst)
return
real_dst = os.path.join(dst, _basename(src))
if os.path.exists(real_dst):
raise Error, "Destination path '%s' already exists" % real_dst
try:
os.rename(src, real_dst)
except OSError:
if os.path.isdir(src):
if _destinsrc(src, dst):
raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
copytree(src, real_dst, symlinks=True)
rmtree(src)
else:
copy2(src, real_dst)
os.unlink(src)
ここでcopytree
、rmtree
とcopy2
は同じモジュール内の他のパブリック関数であり_samefile
、_basename
と と_destinsrc
は同じモジュール内の関数であり、パブリック API の一部であることを意図していません。
結局のところ、Python は Java ではありません。Java では、同じ名前のファイルごとに 1 つのクラスに制限されており、すべてのコードはクラスの一部である必要があります。Python では、クラスは完全にオプションです。