473,395 Members | 1,652 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,395 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 6542
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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...
0
jinu1996
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...

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.