Is there a builtin way of making making another instance of your own
class? I really expected type(self)(*arg s, **keywords) to work this way.
Currently i'm doing this:
def new(self,*args, **keywords):
from new import instance
s=instance(self .__class__)
if hasattr(D(),'__ init__'):
s.__init__(*arg s,**keywords)
return s
--
"You have to impress him! Be independent and plucky, but often do
things that are moronic and out of character!"
--50s Diana Dane to 90s Diana Dane 18 2716
Samuel Kleiner <sa*@samuel-kleiners-computer.local> writes: Is there a builtin way of making making another instance of your own class? I really expected type(self)(*arg s, **keywords) to work this way.
Doesn't self.__class__( *args, **keywords) work?
|>oug
Samuel Kleiner wrote in message ... Is there a builtin way of making making another instance of your own class?
You mean, from the inside (from one of the instance methods of the class)?
def new(self, *args, **kargs):
return self.__class__( *args, **kargs)
I really expected type(self)(*arg s, **keywords) to work this way.
Works for me. What traceback does it give you?
Currently i'm doing this:
def new(self,*args, **keywords): from new import instance s=instance(self .__class__) if hasattr(D(),'__ init__'): s.__init__(*arg s,**keywords) return s
That's ugly.
--
Francis Avila
Francis Avila wrote: Samuel Kleiner wrote in message ...Is there a builtin way of making making another instance of your own class? You mean, from the inside (from one of the instance methods of the class)?
Yes.
def new(self, *args, **kargs): return self.__class__( *args, **kargs)
This works. Thanks. I really expected type(self)(*arg s, **keywords) to work this way.
Works for me. What traceback does it give you?
Not for me. I really want to call it as the function itself, and
type(self)(argu ment1,argument2 ,argument3)
fails with
Traceback (most recent call last):
File "<stdin>", line 139, in ?
File "<stdin>", line 78, in __add__
TypeError: instance() takes at most 2 arguments (3 given)
whereas
self.__class__( argument1,argum ent2,argument3)
does not Currently i'm doing this:
[my code] That's ugly.
Yes.
--
On an encouraging note, however, I found that throughout the source code,
extremely conservative coding practices and good error checking everywhere
means that our software does not crash when handling IPv6 addresses.
--Joe Loughry, Lockheed Martin Space and Strategic Missiles, RADIANT MERCURY
Samuel Kleiner wrote in message ... Not for me. I really want to call it as the function itself, and
type(self)(arg ument1,argument 2,argument3)
fails with
Traceback (most recent call last): File "<stdin>", line 139, in ? File "<stdin>", line 78, in __add__ TypeError: instance() takes at most 2 arguments (3 given)
Ah! You're using classic classes. Don't do that.
Observe: class classic: pass type(classic)
<type 'classobj'> type(classic())
<type 'instance'> classic().__cla ss__
<class __main__.classi c at 0x00F58030>
class newstyle(object ): pass type(newstyle)
<type 'type'> type(newstyle() )
<class '__main__.newst yle'> newstyle().__cl ass__
<class '__main__.newst yle'>
If you're curious, look in the Python Language Reference at the old and new
style classes to see the differences. There's absolutely no advantage to
old style classes, so stop usin' 'em.
There's a grand old intro (by Guido) to new-style classes at python.org,
linked from "What's New" in the 2.3 docs. I keep hunting for that link: it
really should be in the distributed docs, because it's vital for
understanding the still poorly-documented new-style classes.
--
Francis Avila
Francis Avila wrote: Ah! You're using classic classes. Don't do that.
Ok, thanks-
So can I make all my classes derive from object without
doing so explicitly- IE, without having to change every
single top-level class definition?
--
Loren ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh eusmod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation
Samuel Kleiner wrote in message ... Francis Avila wrote:
Ah! You're using classic classes. Don't do that.
Ok, thanks-
So can I make all my classes derive from object without doing so explicitly- IE, without having to change every single top-level class definition?
I doubt it. But I think sed or re could take care of that quite easily with
a search/replace.
Besides, "explicit is better than implicit." ;)
--
Francis Avila
Samuel Kleiner wrote: So can I make all my classes derive from object without doing so explicitly- IE, without having to change every single top-level class definition?
You can do it on a per-file basis by putting
__metaclass__ = type
once before the class definitions.
Peter
Francis Avila wrote: If you're curious, look in the Python Language Reference at the old and new style classes to see the differences. There's absolutely no advantage to old style classes
except performance: http://www.python.org/~jeremy/weblog/030506.html
it's vital for understanding the still poorly-documented new-style classes.
documentation may also be seen as an advantage, of course.
</F>
In article <vt************ @corp.supernews .com>,
Francis Avila <fr***********@ yahoo.com> wrote: If you're curious, look in the Python Language Reference at the old and new style classes to see the differences. There's absolutely no advantage to old style classes, so stop usin' 'em.
<raised eyebrow> In addition to the factors Fredrik mentioned, there's
also the issue that new-style classes are more of a moving target WRT
syntax and semantics; for complex uses, it can be tricky (or impossible)
to get the same code working the same way on both 2.2 and 2.3.
More than that, you *can't* use new-style classes for exceptions. So
please stop telling people to avoid classic classes.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/
Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robert Ferrell |
last post by:
I have a question about assigning __call__ to an instance to make that
instance callable. I know there has been quite a bit of discussion
about this, and I've read all I can find, but I'm still confused.
I'd like to have a factory class that takes a string argument and returns
the appropriate factory method based on that string. I'd like the
instances to be callable. Like this:
fact = Factory('SomeThing')
aSomeThing = fact(some...
|
by: Sridhar R |
last post by:
Consider the code below,
class Base(object):
pass
class Derived(object):
def __new__(cls, *args, **kwds):
# some_factory returns an instance of Base
# and I have to derive from this instance!
|
by: Andre Meyer |
last post by:
Hi all
I have been searching everywhere for this, but have not found a
solution, yet.
What I need is to create an object that is an instance of a class (NOT a
class instance!) of which I only know the name as a string. This what I
tried:
class A:
|
by: |
last post by:
Hi
I have a list containing several instance address, for example:
I'd like to invoke a method on each of these instance but I don't know :
1. if its possible
2. how to proceed
|
by: John M. Gabriele |
last post by:
I've done some C++ and Java in the past, and have recently learned
a fair amount of Python. One thing I still really don't get though
is the difference between class methods and instance methods. I
guess I'll try to narrow it down to a few specific questions, but
any further input offered on the subject is greatly appreciated:
1. Are all of my class's methods supposed to take 'self' as their
first arg?
2. Am I then supposed to call...
| |
by: Göran Tänzer |
last post by:
Hi,
i've written a class which does some calculations for my web application.
These informatinos are different for each page request - the current user is
not important.
i have about 10 aspx pages and 20 ascx user controls. In most of these
pages/user controls i need the informations of this class.
First i created an instance of this class in every page/user control i
|
by: Adam |
last post by:
We have a web site that uses .vb for the web pages and .cs for a class
module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does
work with .NET 1.1.
When trying to access a page that needs the class module I get an error on
web site:
Object reference not set to an instance of an object
Here is where the error is:
|
by: titan nyquist |
last post by:
I have a class with data and methods that use it. Everything is
contained perfectly
THE PROBLEM: A separate thread has to call a method in the current
instantiation of this class. There is only ever ONE instantiation of
this class, and this outside method in a separate thread has to access
it.
How do i do this?
|
by: Ravi gowda |
last post by:
Hi,
i am getting Error on status bar :Applet trustfield Extapp not inited
and web page will stops when loging in to a web site
Please suggest on this issue
Regards,
Ravi,
|
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: 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: 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
| |