Skip to content

Video editing with Python

Avoid mutable default arguments

Cody34db3d38fde5
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index fbab136a..efdfb22c 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -94,7 +94,7 @@ class Clip:
else:
return self.make_frame(t)
- def fl(self, fun, apply_to=[], keep_duration=True):
+ def fl(self, fun, apply_to=None, keep_duration=True):
""" General processing of a clip.
Returns a new Clip whose frames are a transformation
@@ -131,6 +131,8 @@ class Clip:
>>> newclip = clip.fl(fl, apply_to='mask')
"""
+ if apply_to is None:
+ apply_to = []
#mf = copy(self.make_frame)
newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
@@ -153,7 +155,7 @@ class Clip:
- def fl_time(self, t_func, apply_to=[], keep_duration=False):
+ def fl_time(self, t_func, apply_to=None, keep_duration=False):
"""
Returns a Clip instance playing the content of the current clip
but with a modified timeline, time ``t`` being replaced by another
@@ -184,6 +186,8 @@ class Clip:
>>> newclip = clip.fl_time(lambda: 3-t)
"""
+ if apply_to is None:
+ apply_to = []
return self.fl(lambda gf, t: gf(t_func(t)), apply_to,
keep_duration=keep_duration)
diff --git a/moviepy/config.py b/moviepy/config.py
index 307abf90..e102253f 100644
--- a/moviepy/config.py
+++ b/moviepy/config.py
@@ -80,8 +80,10 @@ def get_setting(varname):
return gl[varname]
-def change_settings(new_settings={}, file=None):
+def change_settings(new_settings=None, file=None):
""" Changes the value of configuration variables."""
+ if new_settings is None:
+ new_settings = {}
gl = globals()
if file is not None:
execfile(file)
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index b1c9089d..28caf7d1 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -489,11 +489,13 @@ class VideoClip(Clip):
# IMAGE FILTERS
- def fl_image(self, image_func, apply_to=[]):
+ def fl_image(self, image_func, apply_to=None):
"""
Modifies the images of a clip by replacing the frame
`get_frame(t)` by another frame, `image_func(get_frame(t))`
"""
+ if apply_to is None:
+ apply_to = []
return self.fl(lambda gf, t: image_func(gf(t)), apply_to)
# --------------------------------------------------------------
@@ -918,12 +920,14 @@ class ImageClip(VideoClip):
self.size = img.shape[:2][::-1]
self.img = img
- def fl(self, fl, apply_to=[], keep_duration=True):
+ def fl(self, fl, apply_to=None, keep_duration=True):
"""General transformation filter.
Equivalent to VideoClip.fl . The result is no more an
ImageClip, it has the class VideoClip (since it may be animated)
"""
+ if apply_to is None:
+ apply_to = []
# When we use fl on an image clip it may become animated.
# Therefore the result is not an ImageClip, just a VideoClip.
newclip = VideoClip.fl(self, fl, apply_to=apply_to,
@@ -932,13 +936,15 @@ class ImageClip(VideoClip):
return newclip
@outplace
- def fl_image(self, image_func, apply_to=[]):
+ def fl_image(self, image_func, apply_to=None):
"""Image-transformation filter.
Does the same as VideoClip.fl_image, but for ImageClip the
tranformed clip is computed once and for all at the beginning,
and not for each 'frame'.
"""
+ if apply_to is None:
+ apply_to = []
arr = image_func(self.get_frame(0))
self.size = arr.shape[:2][::-1]
self.make_frame = lambda t: arr
@@ -952,7 +958,7 @@ class ImageClip(VideoClip):
setattr(self, attr, new_a)
@outplace
- def fl_time(self, time_func, apply_to=['mask', 'audio'],
+ def fl_time(self, time_func, apply_to=None,
keep_duration=False):
"""Time-transformation filter.
@@ -962,6 +968,8 @@ class ImageClip(VideoClip):
This method does nothing for ImageClips (but it may affect their
masks or their audios). The result is still an ImageClip.
"""
+ if apply_to is None:
+ apply_to = ['mask', 'audio']
for attr in apply_to:
if hasattr(self, attr):
a = getattr(self, attr)
diff --git a/moviepy/video/fx/blackwhite.py b/moviepy/video/fx/blackwhite.py
index 5d4f1239..140c649c 100644
--- a/moviepy/video/fx/blackwhite.py
+++ b/moviepy/video/fx/blackwhite.py
@@ -1,11 +1,13 @@
import numpy as np
-def blackwhite(clip, RGB = [1,1,1], preserve_luminosity=True):
+def blackwhite(clip, RGB = None, preserve_luminosity=True):
""" Desaturates the picture, makes it black and white.
Parameter RGB allows to set weights for the different color
channels.
If RBG is 'CRT_phosphor' a special set of values is used.
preserve_luminosity maintains the sum of RGB to 1."""
+ if RGB is None:
+ RGB = [1,1,1]
if RGB == 'CRT_phosphor':
RGB = [0.2125, 0.7154, 0.0721]
diff --git a/moviepy/video/fx/mask_color.py b/moviepy/video/fx/mask_color.py
index 8cd30236..b5c102d8 100644
--- a/moviepy/video/fx/mask_color.py
+++ b/moviepy/video/fx/mask_color.py
@@ -1,6 +1,6 @@
import numpy as np
-def mask_color(clip, color=[0,0,0], thr=0, s=1):
+def mask_color(clip, color=None, thr=0, s=1):
""" Returns a new clip with a mask for transparency where the original
clip is of the given color.
@@ -12,6 +12,8 @@ def mask_color(clip, color=[0,0,0], thr=0, s=1):
which is 1 when d>>thr and 0 for d<<thr, the stiffness of the effect being
parametrized by s
"""
+ if color is None:
+ color = [0,0,0]
# code a little sloppy, it just works.
hill = lambda x: (1.0*(x!=0) if (thr==0) else (x**s/ (thr**s+x**s)))
diff --git a/moviepy/video/tools/drawing.py b/moviepy/video/tools/drawing.py
index 7be3bd09..b398708c 100644
--- a/moviepy/video/tools/drawing.py
+++ b/moviepy/video/tools/drawing.py
@@ -5,13 +5,15 @@ methods that are difficult to do with the existing Python libraries.
import numpy as np
-def blit(im1, im2, pos=[0, 0], mask=None, ismask=False):
+def blit(im1, im2, pos=None, mask=None, ismask=False):
""" Blit an image over another.
Blits ``im1`` on ``im2`` as position ``pos=(x,y)``, using the
``mask`` if provided. If ``im1`` and ``im2`` are mask pictures
(2D float arrays) then ``ismask`` must be ``True``.
"""
+ if pos is None:
+ pos = [0, 0]
# xp1,yp1,xp2,yp2 = blit area on im2
# x1,y1,x2,y2 = area of im1 to blit on im2