Spending the morning avoiding responsibilitie s, and seeing what it would
take to color some complex numbers.
class color_complex(c omplex):
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='BLU E')
Traceback (most recent call last):
File "<pyshell#3 7>", line 1, in -toplevel-
a=color_complex (1,7,color='BLU E')
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(c omplex):
def __new__(cls,*ar gs,**kws):
result = complex.__new__ (cls, *args)
result.color = kws.get('color' , 'BLUE')
return result
a=color_complex (1,7,color='BLU E') 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 3 1836
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 responsibilitie s, and seeing what it would take to color some complex numbers.
class color_complex(c omplex): 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='BLU E') Traceback (most recent call last): File "<pyshell#3 7>", line 1, in -toplevel- a=color_complex (1,7,color='BLU E') 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(c omplex): def __new__(cls,*ar gs,**kws): result = complex.__new__ (cls, *args) result.color = kws.get('color' , 'BLUE') return result a=color_complex (1,7,color='BLU E') 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 ==----
Arthur wrote: Spending the morning avoiding responsibilitie s, and seeing what it would take to color some complex numbers.
class color_complex(c omplex): 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
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 responsibilitie s, and seeing what it would take to color some complex numbers.
class color_complex(c omplex): 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='BLU E') Traceback (most recent call last): File "<pyshell#3 7>", line 1, in -toplevel- a=color_complex (1,7,color='BLU E') 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(c omplex):
... 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(c omplex): def __new__(cls,*ar gs,**kws): result = complex.__new__ (cls, *args) result.color = kws.get('color' , 'BLUE') return result
> a=color_complex (1,7,color='BLU E') > 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 them
on my server
(due to my restricted permissions - i can use only PHP and MySQL).
|
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 replacing it with a button of my own background
color and text.
The file paths I'd like displayed in a textarea and then the files uploaded
at once.
|
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.
You can assume xyz < > = and " always exist for each tag.
What methods are usually employed?
|
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 replacing it with a button of my own background
color and text.
The file paths I'd like displayed in a textarea and then the files uploaded
at once.
|
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 that code, which is
coloring text.
Thanks.
| |
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 tags and the like. Is there
any (possibly free!) C-Sharp control (based onto the RTF control, I imagine)
or code sample for building a such control, or should I create my own from
scratch?
Thanks guys!
|
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
|
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 thankful
if someone can tell me where is the problem. I am aware that my code is
far from being efficient and organized, and also there are many extra
#include statements not really required for the code. I am a novice
programmer, as you can see ! At...
|
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
Python?
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |