Why does Rect and Surface have copy method when copy exist in std-lib?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Why does Rect and Surface have copy method when copy exist in std-lib?

Victor Blomqvist
Hello,

Something I have been thinking about:
Rect and Surface classes have their own copy methods. Why do they have that when there is a module called copy in the standard lib that can handle copy (with help)? The rect copy method was added in pygame 1.9 so it is fairly recent.

http://pygame.org/docs/ref/rect.html#pygame.Rect.copy
https://docs.python.org/2/library/copy.html

Thanks for any insights!
/Victor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why does Rect and Surface have copy method when copy exist in std-lib?

Jason Marshall-2
In the C code, rect.copy and surface.copy are equivalent to
rect.__copy__ and surface.__copy__. You may use copy.copy(rect) and
copy.copy(surface) in your code, but copy.copy will simply call
rect.__copy__ or surface.__copy__. By using rect.copy and surface.copy
rather than the standard library's copy.copy, your code will run
≈0.0001% faster.

For aesthetic reasons, you would use rect.copy and surface.copy rather
than rect.__copy__ and surface.__copy__ in your code.

Jason

On Mon, May 29, 2017 at 3:38 PM, Victor Blomqvist <[hidden email]> wrote:

> Hello,
>
> Something I have been thinking about:
> Rect and Surface classes have their own copy methods. Why do they have that
> when there is a module called copy in the standard lib that can handle copy
> (with help)? The rect copy method was added in pygame 1.9 so it is fairly
> recent.
>
> http://pygame.org/docs/ref/rect.html#pygame.Rect.copy
> https://docs.python.org/2/library/copy.html
>
> Thanks for any insights!
> /Victor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why does Rect and Surface have copy method when copy exist in std-lib?

Victor Blomqvist
It seems like such a small performance difference shouldnt affect the decision if a custom copy method is good or not?

/Victor

On Thu, Jun 1, 2017 at 9:24 PM, Jason Marshall <[hidden email]> wrote:
In the C code, rect.copy and surface.copy are equivalent to
rect.__copy__ and surface.__copy__. You may use copy.copy(rect) and
copy.copy(surface) in your code, but copy.copy will simply call
rect.__copy__ or surface.__copy__. By using rect.copy and surface.copy
rather than the standard library's copy.copy, your code will run
≈0.0001% faster.

For aesthetic reasons, you would use rect.copy and surface.copy rather
than rect.__copy__ and surface.__copy__ in your code.

Jason

On Mon, May 29, 2017 at 3:38 PM, Victor Blomqvist <[hidden email]> wrote:
> Hello,
>
> Something I have been thinking about:
> Rect and Surface classes have their own copy methods. Why do they have that
> when there is a module called copy in the standard lib that can handle copy
> (with help)? The rect copy method was added in pygame 1.9 so it is fairly
> recent.
>
> http://pygame.org/docs/ref/rect.html#pygame.Rect.copy
> https://docs.python.org/2/library/copy.html
>
> Thanks for any insights!
> /Victor

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why does Rect and Surface have copy method when copy exist in std-lib?

Daniel Foerster
Allow me to point out that there's such a thing as dict.copy(). It's pointless to import a module to call a method that you know is made publicly available already, and just as pointless to make people import.

On Jun 3, 2017 16:23, "Victor Blomqvist" <[hidden email]> wrote:
It seems like such a small performance difference shouldnt affect the decision if a custom copy method is good or not?

/Victor

On Thu, Jun 1, 2017 at 9:24 PM, Jason Marshall <[hidden email]> wrote:
In the C code, rect.copy and surface.copy are equivalent to
rect.__copy__ and surface.__copy__. You may use copy.copy(rect) and
copy.copy(surface) in your code, but copy.copy will simply call
rect.__copy__ or surface.__copy__. By using rect.copy and surface.copy
rather than the standard library's copy.copy, your code will run
≈0.0001% faster.

For aesthetic reasons, you would use rect.copy and surface.copy rather
than rect.__copy__ and surface.__copy__ in your code.

Jason

On Mon, May 29, 2017 at 3:38 PM, Victor Blomqvist <[hidden email]> wrote:
> Hello,
>
> Something I have been thinking about:
> Rect and Surface classes have their own copy methods. Why do they have that
> when there is a module called copy in the standard lib that can handle copy
> (with help)? The rect copy method was added in pygame 1.9 so it is fairly
> recent.
>
> http://pygame.org/docs/ref/rect.html#pygame.Rect.copy
> https://docs.python.org/2/library/copy.html
>
> Thanks for any insights!
> /Victor

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why does Rect and Surface have copy method when copy exist in std-lib?

Victor Blomqvist
Yes, I have seen that. There is a bunch of other libraries that does it as well, such as numpy.copy pandas.DataFrame.copy.

The reason why I ask is that I have a library (pymunk) where I just implemented pickle support, and I now have the option to either be satisfied with copy.deepcopy which comes for free from pickle, or add a custom copy method just like pygame has done.

To me it seems like having a custom copy goes against the python zen "There should be one-- and preferably only one --obvious way to do it.". At the same time many libraries have custom copy method, just as you noted.


On Sat, Jun 3, 2017 at 11:26 PM, Daniel Foerster <[hidden email]> wrote:
Allow me to point out that there's such a thing as dict.copy(). It's pointless to import a module to call a method that you know is made publicly available already, and just as pointless to make people import.

On Jun 3, 2017 16:23, "Victor Blomqvist" <[hidden email]> wrote:
It seems like such a small performance difference shouldnt affect the decision if a custom copy method is good or not?

/Victor

On Thu, Jun 1, 2017 at 9:24 PM, Jason Marshall <[hidden email]> wrote:
In the C code, rect.copy and surface.copy are equivalent to
rect.__copy__ and surface.__copy__. You may use copy.copy(rect) and
copy.copy(surface) in your code, but copy.copy will simply call
rect.__copy__ or surface.__copy__. By using rect.copy and surface.copy
rather than the standard library's copy.copy, your code will run
≈0.0001% faster.

For aesthetic reasons, you would use rect.copy and surface.copy rather
than rect.__copy__ and surface.__copy__ in your code.

Jason

On Mon, May 29, 2017 at 3:38 PM, Victor Blomqvist <[hidden email]> wrote:
> Hello,
>
> Something I have been thinking about:
> Rect and Surface classes have their own copy methods. Why do they have that
> when there is a module called copy in the standard lib that can handle copy
> (with help)? The rect copy method was added in pygame 1.9 so it is fairly
> recent.
>
> http://pygame.org/docs/ref/rect.html#pygame.Rect.copy
> https://docs.python.org/2/library/copy.html
>
> Thanks for any insights!
> /Victor


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Why does Rect and Surface have copy method when copy exist in std-lib?

Martin Kühne
What we've established so far:

- copy.copy is considered an import "hurdle"
- which is why many modules implement a .copy() function to
- both of which basically wrap an internal .__copy__() function whose
direct use appears to be discouraged

At the end of the day I think you should decide what your code needs
to do: if copying an object or resource is a natural thing to do you
want to provide for your users, implement a __copy__ function. Adding
a line copy = __copy__ underneath would hence be a good thing to do to
encourage and ease use. In some cases, a copy of an object may be
nontrivial to create or, for example with system resource you rather
want to not provide anything like it, don't provide __copy__ and skip
this part.

cheers!
mar77i
Loading...