473,395 Members | 1,383 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.

coloring a complex number

Spending the morning avoiding responsibilities, and seeing what it would
take to color some complex numbers.

class color_complex(complex):
def __init__(self,*args,**kws):
complex.__init__(*args)
self.color=kws.get('color', 'BLUE')
a=color_complex(1,7)
print a (1+7j) #good so far a=color_complex(1,7,color='BLUE') Traceback (most recent call last):
File "<pyshell#37>", line 1, in -toplevel-
a=color_complex(1,7,color='BLUE')
TypeError: 'color' is an invalid keyword argument for this function

No good... it seems that I am actually subclassing the built_in function
'complex' when I am hoping to have been subclassing the built_in numeric
type - complex.

but some googling sends me to lib/test/test_descr.py

where there a working subclass of complex more in
accordance with my intentions.

class color_complex(complex):
def __new__(cls,*args,**kws):
result = complex.__new__(cls, *args)
result.color = kws.get('color', 'BLUE')
return result
a=color_complex(1,7,color='BLUE')
print a (1+7j) print a.color

BLUE

which is very good.

But on the chance that I end up pursuing this road, it would be good if
I understood what I just did. It would certainly help with my
documentation ;)

Assistance appreciated.

NOTE:

The importance of the asset of the depth and breadth of Python archives
- for learning (and teaching) and real world production - should not be
underestimated, IMO. I could be confident if there was an answer to
getting the functionality I was looking for as above, it would be found
easily enough by a google search. It is only with the major
technologies that one can hope to pose a question of almost any kind to
google and get the kind of relevant hits one gets when doing a Python
related search. Python is certainly a major technology, in that
respect. As these archives serve as an extension to the documentation,
the body of Python documentation is beyond any normal expectation.

True, this asset is generally better for answers than explanations.

I got the answer I needed. Pursuing here some explanation of that answer.

Art
Oct 21 '05 #1
3 1808
I'm not 100% sure about this, but from what it seems like, the reason
method B worked, and not method a is because class foo(complex) is
subclassing a metaclass. So if you do this, you can't init a meta class
(try type(complex), it equals 'type' not 'complex'. type(complex())
yields 'complex'), so you use the new operator to generator a class on
the fly which is why it works in method B. I hope that's right.

-Brandon
Spending the morning avoiding responsibilities, and seeing what it would
take to color some complex numbers.

class color_complex(complex):
def __init__(self,*args,**kws):
complex.__init__(*args)
self.color=kws.get('color', 'BLUE')
a=color_complex(1,7)
print a (1+7j) #good so far a=color_complex(1,7,color='BLUE') Traceback (most recent call last):
File "<pyshell#37>", line 1, in -toplevel-
a=color_complex(1,7,color='BLUE')
TypeError: 'color' is an invalid keyword argument for this function

No good... it seems that I am actually subclassing the built_in function
'complex' when I am hoping to have been subclassing the built_in numeric
type - complex.

but some googling sends me to lib/test/test_descr.py

where there a working subclass of complex more in
accordance with my intentions.

class color_complex(complex):
def __new__(cls,*args,**kws):
result = complex.__new__(cls, *args)
result.color = kws.get('color', 'BLUE')
return result
a=color_complex(1,7,color='BLUE')
print a (1+7j) print a.color

BLUE

which is very good.

But on the chance that I end up pursuing this road, it would be good if
I understood what I just did. It would certainly help with my
documentation ;)

Assistance appreciated.

NOTE:

The importance of the asset of the depth and breadth of Python archives
- for learning (and teaching) and real world production - should not be
underestimated, IMO. I could be confident if there was an answer to
getting the functionality I was looking for as above, it would be found
easily enough by a google search. It is only with the major
technologies that one can hope to pose a question of almost any kind to
google and get the kind of relevant hits one gets when doing a Python
related search. Python is certainly a major technology, in that
respect. As these archives serve as an extension to the documentation,
the body of Python documentation is beyond any normal expectation.

True, this asset is generally better for answers than explanations.

I got the answer I needed. Pursuing here some explanation of that answer.

Art

----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Oct 22 '05 #2
Arthur wrote:
Spending the morning avoiding responsibilities, and seeing what it would
take to color some complex numbers.

class color_complex(complex):
def __init__(self,*args,**kws):
complex.__init__(*args)
self.color=kws.get('color', 'BLUE')


In general when you subclass an immutable type you have to override __new__ rather than __init__. There is some explanation and example here:
http://www.python.org/2.2.3/descrintro.html#__new__

Kent
Oct 22 '05 #3
On Fri, 21 Oct 2005 20:55:47 -0500, Brandon K <pr***********@yahoo.com> wrote:
I'm not 100% sure about this, but from what it seems like, the reason
method B worked, and not method a is because class foo(complex) is
subclassing a metaclass. So if you do this, you can't init a meta class
(try type(complex), it equals 'type' not 'complex'. type(complex())
yields 'complex'), so you use the new operator to generator a class on
the fly which is why it works in method B. I hope that's right.

-Brandon
Spending the morning avoiding responsibilities, and seeing what it would
take to color some complex numbers.

class color_complex(complex):
def __init__(self,*args,**kws):
complex.__init__(*args)
self.color=kws.get('color', 'BLUE')
> a=color_complex(1,7)
> print a

(1+7j) #good so far
> a=color_complex(1,7,color='BLUE')

Traceback (most recent call last):
File "<pyshell#37>", line 1, in -toplevel-
a=color_complex(1,7,color='BLUE')
TypeError: 'color' is an invalid keyword argument for this function

No good... it seems that I am actually subclassing the built_in function No, complex is callable, but it's a type:
complex <type 'complex'>
'complex' when I am hoping to have been subclassing the built_in numeric
type - complex.
You need to override __new__ for immutable types, since the args that build
the base object are already used by the time __init__ is called, and UIAM the
default __init__ inherited from object is a noop. However, if you define __init__
you can choose to process the other args in either place, e.g.:
class color_complex(complex): ... def __new__(cls, *args, **kws):
... return complex.__new__(cls, *args)
... def __init__(self, *args, **kws):
... self.color=kws.get('color', 'BLUE')
... a=color_complex(1,7)
a (1+7j) a=color_complex(1,7, color='BLUE')
a (1+7j) a.color 'BLUE'

Or as in what you found, below:
but some googling sends me to lib/test/test_descr.py

where there a working subclass of complex more in
accordance with my intentions.

class color_complex(complex):
def __new__(cls,*args,**kws):
result = complex.__new__(cls, *args)
result.color = kws.get('color', 'BLUE')
return result
> a=color_complex(1,7,color='BLUE')
> print a

(1+7j)
> print a.color

BLUE

which is very good. a=color_complex(1,7, color='RED')
a (1+7j) a.color

'RED'

(just to convince yourself that the default is just a default ;-)


But on the chance that I end up pursuing this road, it would be good if
I understood what I just did. It would certainly help with my
documentation ;)

Assistance appreciated.

NOTE:

The importance of the asset of the depth and breadth of Python archives
- for learning (and teaching) and real world production - should not be
underestimated, IMO. I could be confident if there was an answer to
getting the functionality I was looking for as above, it would be found
easily enough by a google search. It is only with the major
technologies that one can hope to pose a question of almost any kind to
google and get the kind of relevant hits one gets when doing a Python
related search. Python is certainly a major technology, in that
respect. As these archives serve as an extension to the documentation,
the body of Python documentation is beyond any normal expectation.

True, this asset is generally better for answers than explanations.

I got the answer I needed. Pursuing here some explanation of that answer.

HTH

Regards,
Bengt Richter
Oct 22 '05 #4

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

Similar topics

7
by: Ruthless | last post by:
hello. I've got a simple question. I've searched on google for PHP scripts coloring syntax e.g. asm, c, c++, bash etc. I've only found some CGI, apps, modules for Apache - but i can't use...
2
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
1
by: Afanasiy | last post by:
What's the best method for coloring simple markup? <xyz att="val" att="val"> You can assume it is not HTML or XML or valid anything except markup which happens to look somewhat like them. ...
5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
0
by: Markk | last post by:
Hi. I would like to ask If somebody don't have any small sample to coloring XML syntax, e.g. in RichTextBox. It can be in VB.NET or C#. I watched on SharpDevelop, but I could not understand...
2
by: Dan | last post by:
I'd like to show some XML text into an (editable) text box with some syntax coloring for tags, attributes etc. I don't need a full-blown commercial control, I'd just like to do some coloring on...
5
by: Wilfried Mestdagh | last post by:
Hello, I wants to set the background color of rows in a datagridView based on the value of a particular cell. Someone knows how to do this ? -- rgds, Wilfried http://www.mestdagh.biz
12
by: vj | last post by:
Hi! I have a piece of code (shown below) involving complex numbers. The code is not running and giving error ("Invalid floating point operation" and "SQRT:Domain error"). I would be very...
7
by: braver | last post by:
Greetings -- as a long time user of both Python and Ruby interpreters, I got used to the latter's syntax-coloring gem, wirble, which colorizes Ruby syntax on the fly. Is there anything similar for...
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: 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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.