473,385 Members | 1,919 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,385 software developers and data experts.

Upgrading an instance to a subclass

I have a subclass of socket.

class Mysocket (socket):
...

But when I use the python library it will of course
just return an instance of socket, like the SocketServer
module.

So now I was wondering if it is somehow possible to
turn this instance into a Mysocket instance, either
by somehow changing the original instance or producing
a new instance that represents the same connection.

--
Antoon Pardon
Oct 8 '08 #1
2 1501
I would suggest rather than inheriting from socket, encapsulate over it:

class MySocket(object):
def __init__(self, socket):
self.socket = socket

Then you don't have to worry about patching instances...

On Wed, Oct 8, 2008 at 12:54 PM, Antoon Pardon <ap*****@forel.vub.ac.bewrote:
I have a subclass of socket.

class Mysocket (socket):
...

But when I use the python library it will of course
just return an instance of socket, like the SocketServer
module.

So now I was wondering if it is somehow possible to
turn this instance into a Mysocket instance, either
by somehow changing the original instance or producing
a new instance that represents the same connection.

--
Antoon Pardon
--
http://mail.python.org/mailman/listinfo/python-list


--
or*****@orestis.gr
http://orestis.gr
Oct 8 '08 #2
Antoon Pardon wrote:
I have a subclass of socket.

class Mysocket (socket):
...

But when I use the python library it will of course
just return an instance of socket, like the SocketServer
module.

So now I was wondering if it is somehow possible to
turn this instance into a Mysocket instance, either
by somehow changing the original instance
Instances of Python-coded classes can usually have __class__ changed.
But not for builtin, C-coded classes, which I assume
or producing
a new instance that represents the same connection.
Given that socket appears to be immutable, I believe Mysocket would need
a __new__ method that called socket.__new__. But I have never worked
with __new__ functions.

Oct 8 '08 #3

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

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
18
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
1
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
9
by: glomde | last post by:
Hi I wonder if you can set what subclass a class should have at instance creation. The problem is that I have something like: class CoreLang(): def AssignVar(self, var, value): pass class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.