473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to instantiate a different class in a constructor?

Hi all,

I have a class URI and a bunch of derived sub-classes for example
HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
module urllib & friends, however my actual problem however maps very
well to this example).

Now I want to pass a string to constructor of URI() and get an instance
of one of the subclasses back. For example uri=URI('http://abcd/...')
will make 'uri' an instance of HttpURI class, not instance of URI.

To achieve this I have a list of all subclasses of URI and try to
instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
to HttpURI constructor it raises ValueError exception and I want to test
HttpsURI, FtpURI, etc.

For now I have this code:

=====
class URI(object):
def __new__(self, arg):
for subclass in subclasses:
try:
instance = object.__new__(subclass, arg)
return instance
except ValueError, e:
print "Ignoring: %s" % e
raise ValueError("URI format not recognized" % arg)

class HttpURI(URI):
def __init__(self, arg):
if not arg.startswith("http://"):
raise ValueError("%s: not a HTTP URI" % arg)
self._uri = arg

class FtpURI(URI):
def __init__(self, arg):
if not arg.startswith("ftp://"):
raise ValueError("%s: not a FTP URI")
self._uri = arg

subclasses = [HttpURI, FtpURI]

if __name__ == "__main__":
print "Testing HTTP URI"
uri = URI("http://server/path")
print uri

print "Testing FTP URI"
uri = URI("ftp://server/path")
print uri
=====

The problem is that ValueError exception raised in HttpURI.__init__() is
not handled in URI.__new__():

-----
~$ ./tst.py
Testing HTTP URI
<__main__.HttpURI object at 0x808572c> # this is good
Testing FTP URI
Traceback (most recent call last):
File "./tst.py", line 35, in <module>
uri = URI("ftp://server/path")
File "./tst.py", line 18, in __init__
raise ValueError("%s: not a HTTP URI" % arg)
ValueError: ftp://server/path: not a HTTP URI # this is bad
-----

When I change the __init__ methods of subclasses to __new__ I instead get:

-----
../tst.py
Testing HTTP URI
Traceback (most recent call last):
File "./tst.py", line 29, in <module>
uri = URI("http://server/path")
File "./tst.py", line 7, in __new__
instance = object.__new__(subclass, arg)
TypeError: default __new__ takes no parameters
-----

Does anyone have any hints on how to solve this problem? (other than
using urllib or other standard modules - as I said this is just to
demonstrate the nature of my problem).

Thanks!
GiBo
Jan 23 '07 #1
4 1670
I have a class URI and a bunch of derived sub-classes for example
HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
module urllib & friends, however my actual problem however maps very
well to this example).

Now I want to pass a string to constructor of URI() and get an instance
of one of the subclasses back. For example uri=URI('http://abcd/...')
will make 'uri' an instance of HttpURI class, not instance of URI.

To achieve this I have a list of all subclasses of URI and try to
instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
to HttpURI constructor it raises ValueError exception and I want to test
HttpsURI, FtpURI, etc.
<snip/>
Use a factory function:

class UriBase(object):
REGISTRY = {}

class HttpUri(UriBase):
pass

UriBase.REGISTRY['http'] = HttpUri

def URI(arg):
return UriBase.REGISTRY[get_protocol(arg)](arg)

This is untested and could be enhanced by e.g. using metaclasses to perform
the registration automagicall, but I think you get the idea.

Diez
Jan 23 '07 #2
On Jan 23, 5:09 am, GiBo <g...@gentlemail.comwrote:
Hi all,

I have a class URI and a bunch of derived sub-classes for example
HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
module urllib & friends, however my actual problem however maps very
well to this example).

Now I want to pass a string to constructor of URI() and get an instance
of one of the subclasses back. For example uri=URI('http://abcd/...')
will make 'uri' an instance of HttpURI class, not instance of URI.

To achieve this I have a list of all subclasses of URI and try to
instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
to HttpURI constructor it raises ValueError exception and I want to test
HttpsURI, FtpURI, etc.

For now I have this code:

=====
class URI(object):
def __new__(self, arg):
for subclass in subclasses:
try:
instance = object.__new__(subclass, arg)
return instance
except ValueError, e:
print "Ignoring: %s" % e
raise ValueError("URI format not recognized" % arg)
<snip>

Call __new__ and subclass.__init__ explicitly:

class URI(object):
def __new__(self, arg):
for subclass in subclasses:
try:
instance = object.__new__(subclass)
instance.__init__(arg)
return instance
except ValueError, e:
print "Ignoring: %s" % e
raise ValueError("URI format not recognized" % arg)

(Might I suggest 4-space indents vs. 8?)

-- Paul

Jan 23 '07 #3
Paul McGuire wrote:
On Jan 23, 5:09 am, GiBo <g...@gentlemail.comwrote:
>Hi all,

I have a class URI and a bunch of derived sub-classes for example
HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
module urllib & friends, however my actual problem however maps very
well to this example).

Now I want to pass a string to constructor of URI() and get an instance
of one of the subclasses back. For example uri=URI('http://abcd/...')
will make 'uri' an instance of HttpURI class, not instance of URI.

To achieve this I have a list of all subclasses of URI and try to
instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
to HttpURI constructor it raises ValueError exception and I want to test
HttpsURI, FtpURI, etc.

For now I have this code:

=====
class URI(object):
def __new__(self, arg):
for subclass in subclasses:
try:
instance = object.__new__(subclass, arg)
return instance
except ValueError, e:
print "Ignoring: %s" % e
raise ValueError("URI format not recognized" % arg)

<snip>

Call __new__ and subclass.__init__ explicitly:
Thanks! That's it :-)

BTW When is the subclass.__init__() method invoked if I don't explicitly
call it from __new__()? Apparently not from baseclass.__new__() nor from
object.__new__().

GiBo
Jan 23 '07 #4
At Tuesday 23/1/2007 20:07, GiBo wrote:
>BTW When is the subclass.__init__() method invoked if I don't explicitly
call it from __new__()? Apparently not from baseclass.__new__() nor from
object.__new__().
At instance creation, when type(name, bases, ns) is invoked, after
the __new__ call, but only if the returned object is an instance of
type. This happens in function type_call (in typeobject.c)
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
3
by: ernesto | last post by:
Hi everybody: I have the following implementations: class A { public: virtual int GetValue() = 0; };
16
by: gabon | last post by:
Due a big project I would like to create different javascript classes and assign them to divs. But how? :) I know the usage of prototype but given that this could be possible: function...
8
by: julian_m | last post by:
I'm having problems with include. I wrote a small example which shows what's going on... I should say that the problems started after I moved to a shared server. All was working fine in my local...
4
by: Andrew Backer | last post by:
Hello, I am having a problem creating a class dynamically. The class I have is a base class of another, and the parent class has the constructor (which takes one argument). The base class...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
0
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.