Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17222
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: AttributeError in "with" statement (3.2.2) |
| Date | 2011-12-14 08:28 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <13752584.332.1323880138063.JavaMail.geo-discussion-forums@pruu5> (permalink) |
| References | <b8092181-c306-40bc-a07a-bb35bc925cf1@18g2000prn.googlegroups.com> <CALFfu7BwozUsp=WB1+66wH9RR1=DQCAprNve7N+LFR2ptGMFWg@mail.gmail.com> <mailman.3626.1323844169.27778.python-list@python.org> <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> |
On Thursday, December 15, 2011 12:08:32 AM UTC+8, 88888 Dihedral wrote:
> On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote:
> > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote:
> >
> > > To complement what Eric says below: The with statement is looking for an
> > > instance *method*, which by definition, is a function attribute of a
> > > *class* (the class of the context manager) that takes an instance of the
> > > class as its first parameter.
> >
> > I'm not sure that is correct... I don't think that there is anything "by
> > definition" about where methods live. Particularly not in Python where
> > instance methods can be attributes of the instance itself.
> >
> > >>> class Test(object):
> > ... def method(self):
> > ... print("This method is an attribute of the class.")
> > ...
> > >>> t = Test()
> > >>> t.method()
> > This method is an attribute of the class.
> > >>>
> > >>> import types
> > >>> t.method = types.MethodType(
> > ... lambda self: print(
> > ... "This method is an attribute of the instance."), t)
> > >>> t.method()
> > This method is an attribute of the instance.
> >
> >
> > So the normal lookup rules that apply to data attributes, namely
> > instance, then class, then superclasses, also applies to methods in
> > Python. In languages that don't allow that sort of thing, like Java, you
> > need to use convoluted design patterns like Dynamic Proxy to make it
> > work. In Python, you just create a method and attach it on the instance.
> >
> > http://stackoverflow.com/questions/8260740/override-a-method-for-an-
> > instance-of-a-class
> >
> > But this doesn't apply for special dunder attributes like __exit__, for
> > speed reasons. (For new-style classes only, classic classes have no such
> > special casing. This makes automatic delegation a breeze in Python 2 with
> > classic classes, and a PITA in Python 3. Boo hiss.)
> >
> >
> >
> > --
> > Steven
>
> In Python an instance of an object of a class can have its own method.
> A living object can use those methods in the class definition and
> can acquire a new method at runtime.
Therefore, it is possible for an object to build its decision tree of
actions toward a problem of various parameters in the run time.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-13 21:42 -0800
Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-13 23:05 -0700
Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-14 01:29 -0500
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-14 08:01 +0000
Re: AttributeError in "with" statement (3.2.2) 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-14 08:08 -0800
Re: AttributeError in "with" statement (3.2.2) 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-14 08:28 -0800
Re: AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-14 09:16 -0800
Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-14 18:13 -0500
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 05:01 +0000
Re: AttributeError in "with" statement (3.2.2) MRAB <python@mrabarnett.plus.com> - 2011-12-15 05:15 +0000
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 07:21 +0000
Re: AttributeError in "with" statement (3.2.2) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-12-16 09:34 +1300
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-15 07:47 +0000
Re: AttributeError in "with" statement (3.2.2) Steve Howell <showell30@yahoo.com> - 2011-12-15 05:35 -0800
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 03:34 +0000
Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-15 19:39 -0500
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 09:22 +0000
Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-16 17:05 -0500
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-17 01:26 +0000
Re: AttributeError in "with" statement (3.2.2) Terry Reedy <tjreedy@udel.edu> - 2011-12-17 21:09 -0500
Re: AttributeError in "with" statement (3.2.2) Ethan Furman <ethan@stoneleaf.us> - 2011-12-16 15:26 -0800
Re: AttributeError in "with" statement (3.2.2) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-17 03:05 +0000
Re: AttributeError in "with" statement (3.2.2) Ethan Furman <ethan@stoneleaf.us> - 2011-12-16 16:34 -0800
Re: AttributeError in "with" statement (3.2.2) Peter Otten <__peter__@web.de> - 2011-12-14 11:02 +0100
Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-14 09:56 -0700
Re: AttributeError in "with" statement (3.2.2) Lie Ryan <lie.1296@gmail.com> - 2011-12-15 06:14 +1100
Re: AttributeError in "with" statement (3.2.2) Eric Snow <ericsnowcurrently@gmail.com> - 2011-12-14 12:46 -0700
csiph-web