Creating an instance when the argument is already an instance.

Olive diolu at bigfoot.com
Thu Jul 5 06:29:24 EDT 2012


I am learning python -:)

I am creating a new class: package (to analyse the packages database in
some linux distros). I have created a class package such that
package("string") give me an instance of package if string is a correct
representation of a package. I would like that if pack is already an
instance of package then package(pack) just return pack.

This is exactly the behaviour of many of the built-in types. For
example:

[code]
[oesser at pcolivier ~]$ python2
Python 2.7.3 (default, Apr 24 2012, 00:06:13) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=complex(2,3)
>>> b=complex(a)
>>> a is b
True
[/code]

I note here that b is not a new instance of complex, it is another name
for a (as we can see with a is b). I would like to implement such
behaviour but I do not not how.

Olive



More information about the Python-list mailing list