473,385 Members | 1,400 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.

Assigning to self.__class__

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 am I taking advantage of a fortuitous accident, which may get
undone at a future time?

-- Paul
Jan 26 '06 #1
4 6538
Paul McGuire wrote:
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.
Hooray ! You've just (re)discovered the state pattern... for which the
most stupid simple implementation in Python is to :
(snip) assigning to self.__class__, (snip)
!-)

Any comments on this practice?
It can be very confusing for newbies and peoples having no experience
with *dynamic* languages, and I guess control-freaks and
static-typing-addicts would runaway screaming. But I like it anyway !-)
Is this intended capability for Python
objects,
AFAIK, yes.
or am I taking advantage of a fortuitous accident, which may get
undone at a future time?


It's certainly not a fortuitous accident.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jan 26 '06 #2
bruno at modulix wrote:
Paul McGuire wrote:
or am I taking advantage of a fortuitous accident, which may get
undone at a future time?


It's certainly not a fortuitous accident.


And even the (printed) cookbook has examples which assign to
self.__class__... I guess this means this feature isn't going to go away
soo. ;-)

--- Heiko.
Jan 26 '06 #3
Heiko Wundram <mo*******@bit-bukket.org> wrote:
bruno at modulix wrote:
Paul McGuire wrote:
or am I taking advantage of a fortuitous accident, which may get
undone at a future time?


It's certainly not a fortuitous accident.


And even the (printed) cookbook has examples which assign to
self.__class__... I guess this means this feature isn't going to go away


Hmmm, I'm the main decision-maker for what goes in the printed cookbook,
but it's Guido who decides what stays or goes in the language, and it's
not as if we always agree (though I will admit that when we disagree it
later usually turns out he's right, but that's another issue;-). Still,
in this case I don't think we disagree!-)
Alex
Jan 26 '06 #4

"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:_G***************@tornado.texas.rr.com...
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 am I taking advantage of a fortuitous accident, which may get
undone at a future time?


Very little if any of the new class stuff is accidental. As I remember,
..__class__ was originally read-only and was intentionally changed when
practical use cases for rewriting it were developed and presented with a
request for change. Though I could be wrong, I would comfortably use this
feature myself.

Terry J. Reedy

Jan 26 '06 #5

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

Similar topics

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...
6
by: Russell Warren | last post by:
Is there any better way to get a list of the public callables of self other than this? myCallables = classDir = dir(self) for s in classDir: attr = self.__getattribute__(s) if callable(attr)...
2
by: Anton81 | last post by:
Hi! it seems that class Obj: def __init__(self): f=file("obj.dat") self=pickle.load(f) ....
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...
24
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be...
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 < |...
1
by: The Pythonista | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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.