From: Tony R. <To...@br...> - 2009-09-13 14:43:36
|
This is my last measly nickel on the matter :-) : On Sep 12, 2009, at 11:09 PM, "Lenore Horner" <lh...@si...> wrote: > > On Sep 12, 2009, at 19:03 , Tony Risinger wrote: > >> Additionally, many objects that I'm used to working with contain open >> streams, database links, and other singular, resource type >> references. How does one copy these by value? > Sorry. Still baffled. How do you do anything at all with it if you > don't know what it is? However complicated your resource type, it is > at bottom still a string of 0s and 1s is it not? If you cannot read > the sequence to copy it somewhere else, then how can you read it to do > anything else with it? > > If you do > streamB = streamA > where streamB and streamA are now pointing at the same memory > location, > what can you now do that you couldn't do with streamA alone? Sure maybe some ones and zeros eventually, but maybe on a remote machine in a land far far away. A database link cannot be copied; you would either need too reuse the open link, or open a new connection consuming a second slot on the database. > Can someone give me an example where it is necessary to have two names > for the same thing. You are right you could do everything with stream b that you could do with a, since they are the same object. I can't think of a situation where you need to use multiple names in the same scope beyond what Bruce said about simplifying a long name, but, at least how I understand it, that's not the "pass by" part in pass by reference/ value. When you "pass" an object to a method/function, you are essentially giving it a local name/alias inside that method and it's scope. It is now called stream b, even though it's still stream a too, and still working on the same actual object instance. Hence you "passed by reference". > After cudgeling my brain over this for most of the day, the only > possible explanation I have come up with is that you want to scatter > the references around in the memory so that there is always a > reference near enough your other stuff to be read into the register > easily at the same time. In most (prob all) production apps Ive worked on, it's very rare that you would ever need a full copy; usually you create an object and pass it around, doing operations on it, extracting information from it, and so on. It seems that if you're routinely deep copying objects, there might be a better way to think about your problem, but I could be wrong. I'm not super savvy with python, but on other lists I've seen some good explanations. For example, I once read that in python there really isn't "variables"... there are only objects and name bindings/ name spaces. Once you get pass that Python isn't language X, it really does work quite consistently. > > >> >> I am most experienced with php, and since 5.x, all objects are passed >> by reference; one must use the "copy" keyword which would invoke the >> __copy__() special method. >> >> I used the pass stuff around by reference all the time in php, so >> python makes perfect sense to me. Also, pretty much all systems have >> a "copy on write" policy anyway, so your not really passing by value >> until you modify the new reference. > Fine - as long as when I modify the new reference it doesn't change > the original - and vice versa. >> Most new references created are >> read only, and of they are not, I like the python way of making it >> obvious in the code by explicity creating a new object, as in the >> >> b = vector(a) >> >> examples. > Whereas almost everything I do it makes sense only to pass by value > and I expect passage by reference to have a different syntax than > passage by value. Python, however, uses identical syntax whose effect > is object dependent. I would at the very least expect that > a=b > would have the same effect (or complain that the command is invalid) > regardless of what types a and b might be. > >> >> On Sep 12, 2009, at 4:06 PM, "Jacob Schmidt" <schmidjw@rose- >> hulman.edu> wrote: >> >>> There are many reasons for this type of behavior. >>> >>> And there are two sides to this coin. >>> >>> A) Why would you want the names to be references of an object? >>> >>> Imagine you want to pass a custom object into a function. Python >>> doesn't >>> know how to pass it in by value -> it is a distinct instance. >>> >>> For example -> you make a Rational class that you can use for >>> rational >>> number arithmetic. What are you supposed to do when you add two of >>> them >>> together? Change the first one? Change the second one? Make a new >>> one? >>> Alright. That's simple enough. Make a new one. But what if you go >>> deeper, make more complex functions, more complex objects? You can't >>> make a new one every time you want to change something. That's time >>> consuming AND memory consuming. >>> >>> >>> B) Why can't you just copy and pass everything by value? >>> >>> How do you copy an object? Maybe for integers, it is a simple matter >>> of >>> copying the bits into a new object and you're done. How do you >>> copy a >>> list? Do you copy the references of all the items in the list to the >>> new >>> list, or do you try to copy recursively all the items in the list? >>> How >>> do you copy a custom object? What if that object is based on dynamic >>> data? Each object should have a unique identifier, right? But that >>> sometimes defeats the point of the copy. You want the two objects to >>> be >>> identical. >>> >>> Python was written in such a way that it balances these arguments. >>> Tuples and lists are separate entities because they behave >>> differently >>> and are used in the situations for which they are best suited. >>> >>> Assignment just creates a new reference because it is the cheapest >>> quickest way to do it, and in most situations it works. >>> >>> >>> >>> On Sat, 2009-09-12 at 15:07 -0400, Bruce Sherwood wrote: >>>> None of us readers of this mailing list participated in the >>>> creation of Python, >>>> so I doubt that anyone here can tell you why Python is built that >>>> way, nor argue >>>> the merits of the case. Nor was the intent of the experienced.html >>>> article to >>>> explain this behavior, only to alert you to it. >>>> >>>> As for utility of this behavior: It's a bit of a stretch for me, >>>> but I can >>>> imagine a situation where you want to refer to some very long name >>>> by using a >>>> short name, as in >>>> >>>> s = the_sphere_whose_outer_surface_represents_Earth = sphere() >>>> s.pos = vector(10,20,30) >>>> >>>> I tried a little googling to find some history and found this >>>> laconic comment: >>>> >>>> "It is not weird in Python. Implicit copies are never made, partly >>>> because it's very hard to implement it correctly for every object." >>>> >>>> Also, http://merd.sourceforge.net/inoutness.html seems to summarize >>>> these issues >>>> for diverse programming languages. >>>> >>>> Bruce Sherwood >>>> >>>> Lenore Horner wrote: >>>>> >>>>>> http://vpython.org/contents/experienced.html >>>>> >>>>> I'm sorry to say that it was entirely unenlightening. The only >>>>> relevant >>>>> statement seems to be this "Making a second, independent copy of >>>>> an >>>>> object is a fairly unusual thing to do in Python, but there exist >>>>> special methods for doing this." But there is zilch explanation >>>>> of why >>>>> this is so. I repeat: why ever would I want two identical >>>>> objects that >>>>> _must remain so forever_ ? I completely fail to grasp the utility >>>>> of >>>>> this concept. I cannot think of a single thing that I can >>>>> accomplish >>>>> with two identical objects that I cannot accomplish with only one >>>>> of them. >>>>> >>>>> Sorry to be so boneheaded about this, but I just don't get it. >>>>> >>>>> Lenore >>>> >>>> --- >>>> --- >>>> --- >>>> --- >>>> ------------------------------------------------------------------ >>>> Let Crystal Reports handle the reporting - Free Crystal Reports >>>> 2008 30-Day >>>> trial. Simplify your report design, integration and deployment - >>>> and focus on >>>> what you do best, core application coding. Discover what's new with >>>> Crystal Reports now. http://p.sf.net/sfu/bobj-july >>>> _______________________________________________ >>>> Visualpython-users mailing list >>>> Vis...@li... >>>> https://lists.sourceforge.net/lists/listinfo/visualpython-users >>>> >>> >>> >>> --- >>> --- >>> --- >>> --- >>> ------------------------------------------------------------------ >>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 >>> 30-Day >>> trial. Simplify your report design, integration and deployment - and >>> focus on >>> what you do best, core application coding. Discover what's new with >>> Crystal Reports now. http://p.sf.net/sfu/bobj-july >>> _______________________________________________ >>> Visualpython-users mailing list >>> Vis...@li... >>> https://lists.sourceforge.net/lists/listinfo/visualpython-users >>> >>> >> >> >> Tony Risinger >> Application Development Specialist >> Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 >> >> To...@Br... >> >> http://www.brokerbin.com/ >> >> CONFIDENTIAL INFORMATION: This electronic mail message and any >> attached files contain information intended for the exclusive use of >> the specific individual(s) or entity(s) to whom it is addressed and >> may contain information that is propriety, privileged or >> confidential or otherwise exempt from disclosure. If you are not the >> intended recipient, please notify the sender immediately, by reply >> electronic mail or by telephone, of any unintended recipients so we >> may correct our records. Also, delete the original electronic mail >> and any attachments without making any copies of the electronic mail >> message or attachments. >> >> --- >> --- >> --- >> --------------------------------------------------------------------- >> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 >> 30-Day >> trial. Simplify your report design, integration and deployment - and >> focus on >> what you do best, core application coding. Discover what's new with >> Crystal Reports now. http://p.sf.net/sfu/bobj-july >> _______________________________________________ >> Visualpython-users mailing list >> Vis...@li... >> https://lists.sourceforge.net/lists/listinfo/visualpython-users > > Tony Risinger Application Development Specialist Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 To...@Br... http://www.brokerbin.com/ CONFIDENTIAL INFORMATION: This electronic mail message and any attached files contain information intended for the exclusive use of the specific individual(s) or entity(s) to whom it is addressed and may contain information that is propriety, privileged or confidential or otherwise exempt from disclosure. If you are not the intended recipient, please notify the sender immediately, by reply electronic mail or by telephone, of any unintended recipients so we may correct our records. Also, delete the original electronic mail and any attachments without making any copies of the electronic mail message or attachments. |