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.activestat e.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 ** 1 1528
On 6 juin, 19:51, The Pythonista <n...@this.time wrote:
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.activestat e.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
>>> class Foo(object):
... __slots__ = 'a', 'b'
...
>>> class Bar(object):
|
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 scenario:
#------------------------------------------------------------------------------------------
class foo:
tests = {}
|
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 work fine.
(Converting to use of __new__ is not an option - in one case, the change is
temporary, and before the end of the function, I change it back again.)
Any comments on this practice? Is this intended capability for Python
objects, or...
|
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:
x.__getattribute__('name') <==> x.name
|
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
self.intelligence = stats
self.luck = stats
| |
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
|
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 (let's call it CClass) is binded using classical Python
extension API to _PClass, which is accesible through python without
any
|
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, until I
subclassed a class using this technique and from there on things started
screwing up. I finally tracked it down to self.__class__.attribute! What
was happening is that the child classes each over-rode the class
attribute at their level,...
|
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 <
| gabriel.rossetti@arimaz.comwrote:
|
| 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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |