Reputation: 1243
how is it in Python, (sorry for the newbie question), you can do this:
import shutil
shutil.move(..)
that mean can use the move() method straightaway, but when created my own class I have to instantiate it first?
import myclass
print myclass.mymethod(...)
it gives me "unbound method must be called with myclass instance....
Is there a good document I can read on this unbound and bound method? I just want to use the method without instantiation. thanks.
So how shall I code if I just want to use it as is. w/o instantiating?
def mymethod() <----- defined here?
class myclass:
def __init__ ....
def mymethod(self).... <----- define here will give me error w/o instantiation
Upvotes: 0
Views: 433
Reputation: 1122092
You appear to be under the impression that you must put functions in classes.
This is not the case. shutil
is not a class, it is a module. The only classes the shutil
module defines are exceptions; everything else in the documented API is a top-level function. You can take a look at the shutil
sourcecode; the move
function is defined directly in the module source code as:
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)
where copytree
, rmtree
and copy2
are other public functions in the same module, and _samefile
, _basename
and _destinsrc
are functions in the same module that are not meant to be part of the public API.
Python is not Java, after all; Java restricts you to one class per file, with the same name, and all code must be part of a class. In Python, classes are entirely optional.
Upvotes: 2
Reputation: 28380
If your method can be called without a instance to work on then you can add the decorator @static_method
to allow it to be called without an instance. The python manuals covers this well.
Upvotes: 1
Reputation: 4422
You can call the method using the class name as long as you provide the instance as the 1st argument (self). This will work for your class also.
Upvotes: 0