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

Assigning to __class__ : bad form?

I've been wondering for a while about whether assigning to __class__ is
bad form or not. Specifically, I mean doing so when some other method of
implementing the functionality you're after is available (i.e. using an
adapter, or something like the strategy pattern).

To give an example and a non-example of what I'm talking about, consider
the following recipes from the online Python Cookbook:

Ring Buffer: http://aspn.activestate.com/ASPN/Coo...n/Recipe/68429

In this case, I think the assignment to __class__ just obfuscates things,
and the example would be better coded as a single class.

On the other hand,

Fast copy of an object having a slow __init__ : http://
aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66507

This seems like a reasonable use case for assigning to __class__ (except
that it's already implemented in the 'new' module, but, read the Martelli-
bot's comments near the end of the recipe). I consider this a reasonable
use case for assigning to __class__, because, short of the 'new' module,
I don't see any other way to accomplish it.

So, what is the opinion of the broader Python community? Is code that
assigns to __class__ just clever trickiness to be avoided, or is it
sometimes a good thing?

--
code.py: A blog about life, the universe, and Python

http://pythonista.wordpress.com
** Posted from http://www.teranews.com **
Jun 27 '08 #1
1 1500
On 6 juin, 19:51, The Pythonista <n...@this.timewrote:
I've been wondering for a while about whether assigning to __class__ is
bad form or not. Specifically, I mean doing so when some other method of
implementing the functionality you're after is available (i.e. using an
adapter, or something like the strategy pattern).

To give an example and a non-example of what I'm talking about, consider
the following recipes from the online Python Cookbook:

Ring Buffer:http://aspn.activestate.com/ASPN/Coo...n/Recipe/68429

In this case, I think the assignment to __class__ just obfuscates things,
and the example would be better coded as a single class.

On the other hand,

Fast copy of an object having a slow __init__ : http://
aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66507

This seems like a reasonable use case for assigning to __class__ (except
that it's already implemented in the 'new' module, but, read the Martelli-
bot's comments near the end of the recipe). I consider this a reasonable
use case for assigning to __class__, because, short of the 'new' module,
I don't see any other way to accomplish it.

So, what is the opinion of the broader Python community?
My first opinion is that your formulation, ie "assigning *to*
__class__" is perhaps a bit misleading. What you're talking about is
rebinding the __class__ attribute, while, from your subject line, I
thought you were talking about reassigning to (rebinding) a class
attribute from the instance, ie : self.__class__.attrib = value.

Now to the point:
Is code that
assigns to __class__ just clever trickiness to be avoided, or is it
sometimes a good thing?
Both, definitively !-)

Like most of Python's "advanced" features, it's nice to have it
because it can easily solve problems that would otherwise be at best a
PITA, but it's not something you use on a daily basis - nor without
thinking twice. And obviously, there's no clear rule here, except good
taste and common sense. Anyway, the mere fact that you're asking
yourself if it's a good idea in such or such case is a probably a good
indication that you'll find out by yourself the day you'll be tempted
to use this trick whether it's a good or bad idea in this particular
context.
Jun 27 '08 #2

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

Similar topics

1
by: Jp Calderone | last post by:
In trying to update some code built on top of reload(), I've hit a point of confusion in how it is determined whether or not __class__ is allowed to be rebound. Consider, for example: >>>...
13
by: Frans Englich | last post by:
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my...
4
by: Paul McGuire | last post by:
I have some places in pyparsing where I've found that the most straightforward way to adjust an instance's behavior is to change its class. I do this by assigning to self.__class__, and things all...
5
by: Barry Kelly | last post by:
I'm running this version of Python: Python 2.4.3 (#1, May 18 2006, 07:40:45) on cygwin I read in the documentation that these two expressions are interchangeable: ...
16
by: John Salerno | last post by:
Let's say I'm making a game and I have this base class: class Character(object): def __init__(self, name, stats): self.name = name self.strength = stats self.dexterity = stats...
4
by: alf | last post by:
Hi, is there a more elegant way to get o.__class__.__name__. For instance I would imagine name(o). -- alf
1
by: gregory.lielens | last post by:
Hello, We are currently writing python bindings to an existing C++ library, and we encountered a problem that some of you may have solved (or that has found unsolvable :( ): A C++ class...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
0
by: Terry Reedy | last post by:
"Casey McGinty" <casey.mcginty@gmail.comwrote in message news:ae3f58470806051341s75052c29s7adcc4c994462618@mail.gmail.com... | On Thu, Jun 5, 2008 at 5:40 AM, Gabriel Rossetti < |...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.