473,378 Members | 1,623 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,378 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 1501
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 < |...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
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
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...

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.