Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24907

Re: Creating an instance when the argument is already an instance.

References <20120705122924.481a546b@bigfoot.com>
Date 2012-07-05 20:47 +1000
Subject Re: Creating an instance when the argument is already an instance.
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1820.1341485274.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jul 5, 2012 at 8:29 PM, Olive <diolu@bigfoot.com> wrote:
> 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.

One way would be to make the name "package" actually a wrapper
function, not the class itself:

>>> class _package:
	def __init__(self,arg):
		# blah blah
		self.asdf=arg

>>> def package(arg):
	if isinstance(arg,_package): return arg
	return _package(arg)

>>> a=package("Test")
>>> b=package(a)
>>> a is b
True

The leading underscore is a common convention meaning "private
implementation detail".

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Creating an instance when the argument is already an instance. Olive <diolu@bigfoot.com> - 2012-07-05 12:29 +0200
  Re: Creating an instance when the argument is already an instance. Chris Angelico <rosuav@gmail.com> - 2012-07-05 20:47 +1000
    Re: Creating an instance when the argument is already an instance. Hans Mulder <hansmu@xs4all.nl> - 2012-07-05 16:43 +0200
  Re: Creating an instance when the argument is already an instance. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 11:55 +0000
    Re: Creating an instance when the argument is already an instance. Olive <diolu@bigfoot.com> - 2012-07-06 16:01 +0200

csiph-web