Re: Instance vs Class variable oddity

2019-05-18 Thread jfong
Chris Angelico於 2019年5月18日星期六 UTC+8下午3時09分37秒寫道: > On Sat, May 18, 2019 at 1:51 PM wrote: > > > > Correct me if I am wrong, please. > > > > I always think that the LEGB rule (e.g. the namespace to look up for) was > > applied at compile-time, only the binding was resolved "dynamically" at > > ru

Re: Instance vs Class variable oddity

2019-05-18 Thread Chris Angelico
On Sat, May 18, 2019 at 1:51 PM wrote: > > Correct me if I am wrong, please. > > I always think that the LEGB rule (e.g. the namespace to look up for) was > applied at compile-time, only the binding was resolved "dynamically" at > run-time. For example: > > def foo(): > print(x) > > foo() wi

Re: Instance vs Class variable oddity

2019-05-17 Thread dieter
Irv Kalb writes: > ... > The only thing that threw me was that in a line like: > > self.x = self.x + 1 > > in a method, these two uses of self.x can refer to different variables. I > actually teach Python, and this would be a very difficult thing to explain to > students. > > I have never run

Re: Instance vs Class variable oddity

2019-05-17 Thread jfong
Correct me if I am wrong, please. I always think that the LEGB rule (e.g. the namespace to look up for) was applied at compile-time, only the binding was resolved "dynamically" at run-time. For example: def foo(): print(x) foo() will cause a NameError. But after x = 5 foo() will run corr

Re: Instance vs Class variable oddity

2019-05-17 Thread Ethan Furman
On 05/17/2019 11:37 AM, Irv Kalb wrote: self.x = self.x + 1 I have never run across this issue because I would never use the same name as an instance attribute and a class attribute. So you treat your class attributes as if they were static? -- ~Ethan~ -- https://mail.python.org/mailman/lis

Re: Instance vs Class variable oddity

2019-05-17 Thread Chris Angelico
On Sat, May 18, 2019 at 4:40 AM Irv Kalb wrote: > > Thanks for your comments. I am very aware of all the other issues that you > explained. > > The only thing that threw me was that in a line like: > > self.x = self.x + 1 > > in a method, these two uses of self.x can refer to different variables

Re: Instance vs Class variable oddity

2019-05-17 Thread Irv Kalb
> On May 15, 2019, at 5:41 PM, Ben Finney wrote: > > Irv Kalb writes: > >> I just saw some code that confused me. The confusion has to do with >> class variables and instance variables. > > (Perhaps unrelated, but here's another confusion you may be suffering > from: There's no such thing as

Re: Instance vs Class variable oddity

2019-05-15 Thread Ben Finney
Irv Kalb writes: > I just saw some code that confused me. The confusion has to do with > class variables and instance variables. (Perhaps unrelated, but here's another confusion you may be suffering from: There's no such thing as a “class variable” or “instance variable”. In Python, a “variable

Re: Instance vs Class variable oddity

2019-05-15 Thread Irv Kalb
> On May 15, 2019, at 11:02 AM, Rob Gaddi > wrote: > > On 5/15/19 10:52 AM, Irv Kalb wrote: >> I just saw some code that confused me. The confusion has to do with class >> variables and instance variables. In order to understand it better, I built >> this very small example: >> class Test:

Re: Instance vs Class variable oddity

2019-05-15 Thread Rob Gaddi
On 5/15/19 10:52 AM, Irv Kalb wrote: I just saw some code that confused me. The confusion has to do with class variables and instance variables. In order to understand it better, I built this very small example: class Test: x = 0 def __init__(self, id): self.id = id

Re: Instance of 'dict' has no 'replace' member

2019-02-01 Thread Dan Sommers
On 2/1/19 7:15 AM, sinless...@gmail.com wrote: Hello guys can you help me to solve problem when i compile proram got error like this "Instance of 'dict' has no 'replace' member[no member](67;14)". Python dicts don't have a replace method. It looks like you're trying to replace strings inside

Re: Instance variables question

2018-04-16 Thread Steven D'Aprano
On Mon, 16 Apr 2018 09:03:47 -0700, Irv Kalb wrote: > I have been writing OOP code for many years in other languages and for > the past few years in Python. I am writing new curriculum for a course > on OOP in Python. If you're going to be teaching Python, please don't teach terminology which

Re: Instance variables question

2018-04-16 Thread Peter Otten
Chris Angelico wrote: > On Tue, Apr 17, 2018 at 3:34 AM, Peter Otten <__pete...@web.de> wrote: >> Irv Kalb wrote: >> >>> I have been writing OOP code for many years in other languages and for >>> the >>> past few years in Python. I am writing new curriculum for a course on >>> OOP >>> in Python.

Re: Instance variables question

2018-04-16 Thread Chris Angelico
On Tue, Apr 17, 2018 at 3:34 AM, Peter Otten <__pete...@web.de> wrote: > Irv Kalb wrote: > >> I have been writing OOP code for many years in other languages and for the >> past few years in Python. I am writing new curriculum for a course on OOP >> in Python. In order to see how others are explai

Re: Instance variables question

2018-04-16 Thread Irv Kalb
> On Apr 16, 2018, at 9:48 AM, duncan smith wrote: > > On 16/04/18 17:03, Irv Kalb wrote: >> I have been writing OOP code for many years in other languages and for the >> past few years in Python. I am writing new curriculum for a course on OOP >> in Python. In order to see how others are ex

Re: Instance variables question

2018-04-16 Thread Peter Otten
Irv Kalb wrote: > I have been writing OOP code for many years in other languages and for the > past few years in Python. I am writing new curriculum for a course on OOP > in Python. In order to see how others are explaining OOP concepts, I have > been reading as many books and watching as many v

Re: Instance variables question

2018-04-16 Thread duncan smith
On 16/04/18 17:03, Irv Kalb wrote: > I have been writing OOP code for many years in other languages and for the > past few years in Python. I am writing new curriculum for a course on OOP in > Python. In order to see how others are explaining OOP concepts, I have been > reading as many books a

Re: Instance variables question

2018-04-16 Thread Thomas Jollans
On 2018-04-16 18:03, Irv Kalb wrote: > He gives a demonstration using the following example: > > class PartyAnimal(): > x = 0 > > def party(self): > self.x = self.x + 1 > print('So far', self.x) > > [snip] > > But there is something there that seems odd. My understandin

Re: Instance variables question

2018-04-16 Thread Joseph L. Casale
From: Python-list on behalf of Irv Kalb Sent: Monday, April 16, 2018 10:03 AM To: python-list@python.org Subject: Instance variables question   > class PartyAnimal(): >     x = 0 > >     def party(self): >     self.x = self.x + 1 >     print('So far', self.x) Your not accessing the

Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Steven D'Aprano
On Sat, 3 Oct 2015 04:35 pm, neubyr wrote: > I was wondering if there is any resource that explains why certain methods > like str() and type() were implemented the way they are, rather than > .to_string() or .type() instance/object methods. There is a FAQ that might help with this question: htt

Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Terry Reedy
On 10/3/2015 2:35 AM, neubyr wrote: I was wondering if there is any resource that explains why certain methods like str() and type() These are classes. Calling a class calls the class construction and initialization functions. These return an instance of the class. While reading the tutoria

Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Laura Creighton
In a message of Fri, 02 Oct 2015 23:35:28 -0700, neubyr writes: >I was wondering if there is any resource that explains why certain methods >like str() and type() were implemented the way they are, rather than >.to_string() or .type() instance/object methods. > >I find instance/object methods more

Re: instance as module

2015-06-22 Thread Robin Becker
On 20/06/2015 08:24, Steven D'Aprano wrote: On Fri, 19 Jun 2015 07:29 pm, Robin Becker wrote: I'm trying to overcome a recursive import issue in reportlab. .. I'm afraid I don't understand what you are trying to say here. Why can't the user just set up "such a default" e.g. canvas_ba

Re: instance as module

2015-06-20 Thread Steven D'Aprano
On Fri, 19 Jun 2015 07:29 pm, Robin Becker wrote: > I'm trying to overcome a recursive import issue in reportlab. > > Module reportlab.rl_config uses various sources (eg ~/.reportlab_settings) > to initialize various defaults eg canvas_basefontname. If a user wants to > utilize reportlab to set u

Re: instance as module

2015-06-19 Thread dieter
Robin Becker writes: > I'm trying to overcome a recursive import issue in reportlab. > ... sketched solution ... In the "zope" project, the same problem was approached in a slightly different way -- see the product "zope.deferredimport". It allows to defer an actual import for an imported name u

Re: instance as module

2015-06-19 Thread Ben Finney
Robin Becker writes: > For the specific case of the canvas_basefontname I don't actually need > to do anything specific since it is just a string The configuration values should be nothing but immutable values: strings, integers, booleans. Possibly collections of those. You seem to be implying

Re: instance as module

2015-06-19 Thread Robin Becker
On 19/06/2015 11:23, Peter Otten wrote: Robin Becker wrote: . Do I understand this correctly? You got bitten by a complex setup and now you are hoping to improve the situation by making it even more complex? How about reordering initialisation in such a way that the user defaults are

Re: instance as module

2015-06-19 Thread Peter Otten
Robin Becker wrote: > I'm trying to overcome a recursive import issue in reportlab. > > Module reportlab.rl_config uses various sources (eg ~/.reportlab_settings) > to initialize various defaults eg canvas_basefontname. If a user wants to > utilize reportlab to set up such a default, it's not pos

Re: instance attribute "a" defined outside __init__

2015-04-01 Thread John Gordon
In Rio writes: > Hi, When running below code, I get error saying: > instance attribute "a" defined outside __init__ That's a warning, not an error. And it's a warning from pylint, not from Python itself. It's trying to suggest better style, that's all. It's unusual to define instance vari

Re: instance has no __call__ method

2010-12-11 Thread Steve Holden
On 12/10/2010 5:20 AM, frank cui wrote: > Hi all, > > I'm a novice learner of python and get caught in the following trouble > and hope experienced users can help me solve it:) > > Code: > --- > $ cat Muffle_ZeroDivision.py > #!/

Re: instance has no __call__ method

2010-12-10 Thread Dave Angel
On 01/-10/-28163 02:59 PM, frank cui wrote: Hi all, I'm a novice learner of python and get caught in the following trouble and hope experienced users can help me solve it:) Code: --- $ cat Muffle_ZeroDivision.py #!/usr/bin/env

Re: Instance method decorator garbage collection problem

2010-06-23 Thread Thomas Jollans
On 06/23/2010 02:56 PM, John Reid wrote: > > > Thomas Jollans wrote: >>> The InstanceCounted.count is 1 at the end. If I omit the call to >>> "self.method = print_exception_decorator(self.method)" then the instance >>> count goes down to 0 as desired. I thought that the decorator might be >>> hol

Re: Instance method decorator garbage collection problem

2010-06-23 Thread John Reid
Thomas Jollans wrote: The InstanceCounted.count is 1 at the end. If I omit the call to "self.method = print_exception_decorator(self.method)" then the instance count goes down to 0 as desired. I thought that the decorator might be holding a reference to the instance through the bound method, so

Re: Instance method decorator garbage collection problem

2010-06-23 Thread Christian Heimes
> The InstanceCounted.count is 1 at the end. If I omit the call to > "self.method = print_exception_decorator(self.method)" then the instance > count goes down to 0 as desired. I thought that the decorator might be > holding a reference to the instance through the bound method, so I added > t

Re: Instance method decorator garbage collection problem

2010-06-23 Thread Peter Otten
John Reid wrote: > Hi, > > I've written a decorator that prints exceptions and I'm having some > trouble with garbage collection. > > My decorator is: > > import sys > def print_exception_decorator(fn): > def decorator(self, *args, **kwds): > try: > return fn(*args, *

Re: Instance method decorator garbage collection problem

2010-06-23 Thread Thomas Jollans
On 06/23/2010 01:40 PM, John Reid wrote: > Hi, > > I've written a decorator that prints exceptions and I'm having some > trouble with garbage collection. > > My decorator is: > > import sys > def print_exception_decorator(fn): > def decorator(self, *args, **kwds): > try: >

Re: Instance factory - am I doing this right?

2010-03-04 Thread eb303
On Mar 3, 6:41 pm, Laszlo Nagy wrote: > This is just an interesting code pattern that I have recently used: > > class CacheStorage(object): >     """Generic cache storage class.""" >     @classmethod >     def get_factory(cls,*args,**kwargs): >         """Create factory for a given set of cache st

Re: Instance attributes vs method arguments

2008-11-26 Thread Aahz
In article <[EMAIL PROTECTED]>, M.-A. Lemburg <[EMAIL PROTECTED]> wrote: > >It is always good practice to provide default values for instance >variables in the class definition, both to enhance readability and to >allow adding documentation regarding the variables, e.g. Actually, my company uses a

Re: Instance attributes vs method arguments

2008-11-25 Thread John O'Hagan
On Wed, 26 Nov 2008, Ben Finney wrote: > John O'Hagan <[EMAIL PROTECTED]> writes: > > insofar as one is only interested in accessing methods, is there an > > difference in efficiency (for large enough number of methods and > > arguments) between > > > > a) passing all arguments to __init__() and ac

Re: Instance attributes vs method arguments

2008-11-25 Thread Steven D'Aprano
On Tue, 25 Nov 2008 10:21:18 +0100, M.-A. Lemburg wrote: > It is always good practice to provide default values for instance > variables in the class definition, both to enhance readability and to > allow adding documentation regarding the variables, e.g. > > class Class_a: > ># Foo bar >

Re: Instance attributes vs method arguments

2008-11-25 Thread Ben Finney
John O'Hagan <[EMAIL PROTECTED]> writes: > insofar as one is only interested in accessing methods, is there an > difference in efficiency (for large enough number of methods and > arguments) between > > a) passing all arguments to __init__() and accessing them via self > within individual methods

Re: Instance attributes vs method arguments

2008-11-25 Thread George Sakkis
On Nov 25, 8:49 pm, John O'Hagan <[EMAIL PROTECTED]> wrote: > is there an > difference in efficiency (for large enough number of methods and arguments) > between > > a) passing all arguments to __init__() and accessing them via self within > individual methods: > > class = Class(all_class_args) >

Re: Instance attributes vs method arguments

2008-11-25 Thread John O'Hagan
On Tue, 25 Nov 2008, Rafe wrote: > On Nov 25, 5:48 pm, John O'Hagan <[EMAIL PROTECTED]> wrote: > > On Tue, 25 Nov 2008, Marc 'BlackJack' Rintsch wrote: > > > On Tue, 25 Nov 2008 07:27:41 +, John O'Hagan wrote: > > > > Is it better to do this: > > > > > > > > class Class_a(): > > > >       def _

Re: Instance attributes vs method arguments

2008-11-25 Thread Bruno Desthuilliers
M.-A. Lemburg a écrit : (snip) It is always good practice to provide default values for instance variables in the class definition, both to enhance readability and to allow adding documentation regarding the variables, e.g. Your opinion. As far as I'm concerned, using class variables this way i

Re: Instance attributes vs method arguments

2008-11-25 Thread Rafe
On Nov 25, 5:48 pm, John O'Hagan <[EMAIL PROTECTED]> wrote: > On Tue, 25 Nov 2008, Marc 'BlackJack' Rintsch wrote: > > On Tue, 25 Nov 2008 07:27:41 +, John O'Hagan wrote: > > > Is it better to do this: > > > > class Class_a(): > > >       def __init__(self, args): > > >               self.a = a

Re: Instance attributes vs method arguments

2008-11-25 Thread Marc 'BlackJack' Rintsch
On Tue, 25 Nov 2008 10:48:01 +, John O'Hagan wrote: > On Tue, 25 Nov 2008, Marc 'BlackJack' Rintsch wrote: >> On Tue, 25 Nov 2008 07:27:41 +, John O'Hagan wrote: >> > Is it better to do this: >> > >> > class Class_a(): >> >def __init__(self, args): >> >self.a = args.a >> >

Re: Instance attributes vs method arguments

2008-11-25 Thread John O'Hagan
On Tue, 25 Nov 2008, Marc 'BlackJack' Rintsch wrote: > On Tue, 25 Nov 2008 07:27:41 +, John O'Hagan wrote: > > Is it better to do this: > > > > class Class_a(): > > def __init__(self, args): > > self.a = args.a > > self.b = args.b > > self.c = args.c > >

Re: Instance attributes vs method arguments

2008-11-25 Thread Rafe
snip > It is always good practice to provide default values for > instance variables in the class definition, both to enhance > readability and to allow adding documentation regarding > the variables, e.g. > > class Class_a: > >    # Foo bar >    a = None > >    # Foo baz >    b = None snip Those

Re: Instance attributes vs method arguments

2008-11-25 Thread M.-A. Lemburg
On 2008-11-25 08:27, John O'Hagan wrote: > > Is it better to do this: > > class Class_a(): > def __init__(self, args): > self.a = args.a > self.b = args.b > self.c = args.c > self.d = args.d > def method_ab(self): >

Re: Instance attributes vs method arguments

2008-11-25 Thread bieffe62
On 25 Nov, 08:27, John O'Hagan <[EMAIL PROTECTED]> wrote: > Is it better to do this: > > class Class_a(): >         def __init__(self, args): >                 self.a = args.a         >                 self.b = args.b >                 self.c = args.c >                 self.d = args.d >         def

Re: Instance attributes vs method arguments

2008-11-25 Thread Marc 'BlackJack' Rintsch
On Tue, 25 Nov 2008 07:27:41 +, John O'Hagan wrote: > Is it better to do this: > > class Class_a(): > def __init__(self, args): > self.a = args.a > self.b = args.b > self.c = args.c > self.d = args.d > def method_ab(self): >

Re: instance comparison

2008-07-24 Thread Petite Abeille
On Jul 24, 2008, at 7:53 PM, King wrote: The the class is not subclass of another one. Problem still persist. The code is pretty huge and I am trying to post the information as clear as possible. Mark V. Shaney, from Dissociated Press, I presume? -- PA. http://alt.textdrive.com/nanoki/ -- ht

Re: instance comparison

2008-07-24 Thread Fredrik Lundh
King skrev: The the class is not subclass of another one. Problem still persist. The code is pretty huge and I am trying to post the information as clear as possible. feel free to *add* stuff to the following example until it breaks, if that's easier: >>> class Spam: ... def __init__(se

Re: instance comparison

2008-07-24 Thread King
No, The the class is not subclass of another one. Problem still persist. The code is pretty huge and I am trying to post the information as clear as possible. -- http://mail.python.org/mailman/listinfo/python-list

Re: instance comparison

2008-07-24 Thread Bruno Desthuilliers
King a écrit : The only methods I do have in class is __init__ and __str__. Is your class subclassing another one ? -- http://mail.python.org/mailman/listinfo/python-list

Re: instance comparison

2008-07-24 Thread Fredrik Lundh
King wrote: The only methods I do have in class is __init__ and __str__. How ever inst1 and inst2 is coming from a dictionary where I stored them with a unique id. inst1 = stored[id] inst2 = stored[id] Is this makes a difference? unlikely (well, if that's the literal code, both variables wil

Re: instance comparison

2008-07-24 Thread King
The only methods I do have in class is __init__ and __str__. How ever inst1 and inst2 is coming from a dictionary where I stored them with a unique id. inst1 = stored[id] inst2 = stored[id] Is this makes a difference? I will rip down the piece of code giving me problem and post. -- http://mail.p

Re: instance comparison

2008-07-24 Thread Fredrik Lundh
King wrote: Is this mean when you have overridden __str__ method then it comapre with results of __str__ or else it will comapre whether they are the same instances? Comparisons uses __cmp__ or the rich comparison set; see http://docs.python.org/ref/customization.html for details. Sets

Re: instance comparison

2008-07-24 Thread John Machin
On Jul 24, 6:50 pm, King <[EMAIL PROTECTED]> wrote: > I am facing a problem where I am really confused about it. > > I am trying to compare to instances using: > > if inst1 == inst2 > > These instances have a overridden method __str__ which returns same > string. The condition result is true althou

Re: Instance

2008-07-22 Thread karthikbalaguru
On Jul 17, 5:34 pm, "Calvin Spealman" <[EMAIL PROTECTED]> wrote: > On Thu, Jul 17, 2008 at 2:56 AM, karthikbalaguru > > <[EMAIL PROTECTED]> wrote: > > Hi, > > > I am new to python. I am trying to use the python files given to me > > for bringing up a setup. > > I get the following error while tryin

Re: Instance

2008-07-17 Thread Calvin Spealman
On Thu, Jul 17, 2008 at 2:56 AM, karthikbalaguru <[EMAIL PROTECTED]> wrote: > Hi, > > I am new to python. I am trying to use the python files given to me > for bringing up a setup. > I get the following error while trying to use a python file - > AttributeError : Classroom instance has no attribute

Re: Instance

2008-07-17 Thread Jeroen Ruigrok van der Werven
-On [20080717 09:01], karthikbalaguru ([EMAIL PROTECTED]) wrote: >AttributeError : Classroom instance has no attribute 'desk_offset' You are using a Classroom instance and probably assigning something to the instance's variable/attribute 'desk_offset'. Except that the class Classroom has no self.d

Re: Instance Names

2008-07-03 Thread Larry Bates
Tim Cook wrote: On Thu, 2008-07-03 at 14:20 -0500, Larry Bates wrote: I suspect there is some "misunderstanding" here. Why exactly do you think you need to have your instances named with [] characters in them? I often misunderstand. :-) But, I am implementing specifications in Python tha

Re: Instance Names

2008-07-03 Thread Tim Cook
On Thu, 2008-07-03 at 14:20 -0500, Larry Bates wrote: > I suspect there is some "misunderstanding" here. Why exactly do you think > you > need to have your instances named with [] characters in them? > I often misunderstand. :-) But, I am implementing specifications in Python that are alre

Re: Instance Names

2008-07-03 Thread Larry Bates
Tim Cook wrote: Hi All, I have a need (if at all possible) to create instance names using '[' and ']', i.e. [at]=ClassA0(), [at0001]=ClassB2(), etc. Of course Python tries to unpack a sequence when I do that. Is there anyway to do this? I do have a workaround but it is an ugly, nasty URL

Re: Instance of class "object"

2008-05-17 Thread castironpi
On May 17, 1:09 am, Carl Banks <[EMAIL PROTECTED]> wrote: > On May 16, 4:46 am, "甜瓜" <[EMAIL PROTECTED]> wrote: > > > Howdy, > > I wonder why below does not work. > > > a = object() > > a.b = 1# dynamic bind attribute failed... > > > To make it correct, we have to create a new class: >

Re: Instance of class "object"

2008-05-16 Thread Carl Banks
On May 16, 4:46 am, "甜瓜" <[EMAIL PROTECTED]> wrote: > Howdy, > I wonder why below does not work. > > a = object() > a.b = 1# dynamic bind attribute failed... > > To make it correct, we have to create a new class: > class MyClass(object): pass > a = MyClass() > a.b = 1 # OK > > Doe

Re: Instance of class "object"

2008-05-16 Thread Terry Reedy
"Ìð¹Ï" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | > # an efficient 'Pair' class holding two objects | > class Pair(object): | >__slots__ = 'first', 'second' | > | > Instances of Pair take up even less room that 2-element tuples | > because they don't carry the size informat

Re: Instance of class "object"

2008-05-16 Thread Hrvoje Niksic
"甜瓜" <[EMAIL PROTECTED]> writes: >> # an efficient 'Pair' class holding two objects >> class Pair(object): >>__slots__ = 'first', 'second' >> >> Instances of Pair take up even less room that 2-element tuples >> because they don't carry the size information in the object. >> >> Now, if the obje

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 4:26 am, Peter Otten <[EMAIL PROTECTED]> wrote: > 甜瓜 wrote: > > I wonder why below does not work. > > > a = object() > > a.b = 1# dynamic bind attribute failed... > > The implementation of slots depends on that behaviour: > > http://docs.python.org/ref/slots.html > > > Does t

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 10:21 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 16, 9:41 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > > > > On May 16, 8:56 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > On May 16, 8:51 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > > On May 16, 4:26 am, Peter Otten

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 9:41 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 16, 8:56 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > > > > On May 16, 8:51 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > On May 16, 4:26 am, Peter Otten <[EMAIL PROTECTED]> wrote: > > > > > 甜瓜 wrote: > > > > > I wonder

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 8:56 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 16, 8:51 am, castironpi <[EMAIL PROTECTED]> wrote: > > > > > > > On May 16, 4:26 am, Peter Otten <[EMAIL PROTECTED]> wrote: > > > > 甜瓜 wrote: > > > > I wonder why below does not work. > > > > > a = object() > > > > a.b = 1

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 4:16 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > "甜瓜" <[EMAIL PROTECTED]> writes: > > Howdy, > > I wonder why below does not work. > > > a = object() > > a.b = 1# dynamic bind attribute failed... > > Because the default object class doesn't have a dict or other > indicatio

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 8:51 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 16, 4:26 am, Peter Otten <[EMAIL PROTECTED]> wrote: > > > 甜瓜 wrote: > > > I wonder why below does not work. > > > > a = object() > > > a.b = 1# dynamic bind attribute failed... > > > The implementation of slots depends o

Re: Instance of class "object"

2008-05-16 Thread castironpi
On May 16, 4:26 am, Peter Otten <[EMAIL PROTECTED]> wrote: > 甜瓜 wrote: > > I wonder why below does not work. > > > a = object() > > a.b = 1# dynamic bind attribute failed... > > The implementation of slots depends on that behaviour: > > http://docs.python.org/ref/slots.html > > > Does t

Re: Instance of class "object"

2008-05-16 Thread 甜瓜
2008/5/16 Hrvoje Niksic <[EMAIL PROTECTED]>: > "甜瓜" <[EMAIL PROTECTED]> writes: > >> Howdy, >> I wonder why below does not work. >> >> a = object() >> a.b = 1# dynamic bind attribute failed... > > Because the default object class doesn't have a dict or other > indication of state. It's

Re: Instance of class "object"

2008-05-16 Thread Peter Otten
甜瓜 wrote: > I wonder why below does not work. > > a = object() > a.b = 1# dynamic bind attribute failed... The implementation of slots depends on that behaviour: http://docs.python.org/ref/slots.html > Does this strange behavior break the LSP (Liskov substitution principle)? Can y

Re: Instance of class "object"

2008-05-16 Thread Hrvoje Niksic
"甜瓜" <[EMAIL PROTECTED]> writes: > Howdy, > I wonder why below does not work. > > a = object() > a.b = 1# dynamic bind attribute failed... Because the default object class doesn't have a dict or other indication of state. It's a "pure" Python object whose only visible properties are

Re: Instance of class "object"

2008-05-16 Thread Ben Finney
"甜瓜" <[EMAIL PROTECTED]> writes: > Howdy, > I wonder why below does not work. > > a = object() > a.b = 1# dynamic bind attribute failed... > > To make it correct, we have to create a new class: > class MyClass(object): pass > a = MyClass() > a.b = 1 # OK It's annoyingly diffic

Re: Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread Diez B. Roggisch
mrstephengross schrieb: >> class Foo: >> foo = Foo() >> >> You have to live with that. Just do >> Outer.foo = Outer.Parent() >> after your class-statement to achieve the same result. > > Hmmm. Well, I see why that works. It's too bad, though. If I want to > keep all executed code safely with

Re: Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread Gabriel Genellina
En Wed, 27 Feb 2008 16:52:57 -0200, mrstephengross <[EMAIL PROTECTED]> escribi�: >> class Foo: >> foo = Foo() >> >> You have to live with that. Just do >> Outer.foo = Outer.Parent() >> after your class-statement to achieve the same result. > > Hmmm. Well, I see why that works. It's too bad

Re: Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread mrstephengross
> class Foo: > foo = Foo() > > You have to live with that. Just do > Outer.foo = Outer.Parent() > after your class-statement to achieve the same result. Hmmm. Well, I see why that works. It's too bad, though. If I want to keep all executed code safely within a "if __name__ == '__main__'" blo

Re: Instance of inherited nested class in outer class not allowed?

2008-02-27 Thread Diez B. Roggisch
mrstephengross schrieb: > I've got an interesting problem with my class hierarchy. I have an > outer class, in which two nested classes are defined: > > class Outer: > class Parent: > def __init__ (self): > print "parent!" > class Child(Parent): > def __init__ (self): > Out

Re: instance as a sequence

2007-11-05 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > On Nov 5, 9:40 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > >>On Nov 5, 11:32 am, [EMAIL PROTECTED] wrote: >> >> >>>suppose i want to >>>make foo.childNodes[bar] available as foo[bar] >>>(while still providing access to the printxml/printprettyxml() >>>functions >>>a

Re: instance as a sequence

2007-11-05 Thread Matimus
On Nov 5, 9:59 am, [EMAIL PROTECTED] wrote: > On Nov 5, 9:40 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > > > On Nov 5, 11:32 am, [EMAIL PROTECTED] wrote: > > > > suppose i want to > > > make foo.childNodes[bar] available as foo[bar] > > > (while still providing access to the printxml/printprettyx

Re: instance as a sequence

2007-11-05 Thread sndive
On Nov 5, 9:40 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Nov 5, 11:32 am, [EMAIL PROTECTED] wrote: > > > suppose i want to > > make foo.childNodes[bar] available as foo[bar] > > (while still providing access to the printxml/printprettyxml() > > functions > > and other functionality of dom/mi

Re: instance as a sequence

2007-11-05 Thread Paul McGuire
On Nov 5, 11:32 am, [EMAIL PROTECTED] wrote: > suppose i want to > make foo.childNodes[bar] available as foo[bar] > (while still providing access to the printxml/printprettyxml() > functions > and other functionality of dom/minidom instance). > > What is a good way to accomplish that? define __get

Re: instance as a sequence

2007-11-05 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote: > suppose i want to > make foo.childNodes[bar] available as foo[bar] > (while still providing access to the printxml/printprettyxml() > functions > and other functionality of dom/minidom instance). > > What is a good way to accomplish that? Using element-tree. That alre

Re: instance method questions

2007-08-22 Thread Peter Otten
Danny wrote: > I have created an instance method for an object using new.instancemethod. > It works great. Now the questions are: > > 1) how do I dynamically inspect an object to determine if it has an > instance method? (there is a class method with the same name) Why would you want to do that?

Re: instance method questions

2007-08-22 Thread Frederick Polgardy
On Aug 22, 10:23 am, Danny <[EMAIL PROTECTED]> wrote: > 1) how do I dynamically inspect an object to determine if it has an instance > method? (there is a class method with the same name) class Foo: def foo(self): pass x = Foo() import types >>> isinstance(x.foo, types.MethodType) T

Re: instance variable weirdness

2006-04-14 Thread Felipe Almeida Lessa
Em Sáb, 2006-04-15 às 04:03 +1000, Steven D'Aprano escreveu: > Sometimes you want the default to mutate each time it is used, for example > that is a good technique for caching a result: > > def fact(n, _cache=[1, 1, 2]): > "Iterative factorial with a cache." > try: > return _cache

Re: instance variable weirdness

2006-04-14 Thread Steven D'Aprano
On Fri, 14 Apr 2006 13:30:49 -0300, Felipe Almeida Lessa wrote: > Em Sex, 2006-04-14 às 09:18 -0700, wietse escreveu: >> def __init__(self, name, collection=[]): > > Never, ever, use the default as a list. Unless you want to use the default as a list. Sometimes you want the default to mutat

Re: instance variable weirdness

2006-04-14 Thread Felipe Almeida Lessa
Em Sex, 2006-04-14 às 13:30 -0300, Felipe Almeida Lessa escreveu: > To solve your problem, change > def __init__(self, name, collection=[]): > BaseClass.__init__(self) > self.name = name > self.collection = collection # Will reuse the list > to > def __init__(self,

Re: instance variable weirdness

2006-04-14 Thread Felipe Almeida Lessa
Em Sex, 2006-04-14 às 09:18 -0700, wietse escreveu: > def __init__(self, name, collection=[]): Never, ever, use the default as a list. > self.collection = collection This will just make a reference of self.collection to the collection argument. > inst.collection.append(i) A

Re: instance and class-hierarchy ?

2006-03-29 Thread I V
Bror Johansson wrote: > Is there a good and general way to test an instance-object obj for having a > class belonging to a certain "sub-tree" of the hierarchy with a common > parent class C? If I understand you correctly, isinstance ought to do the job: class A(object): pass class B(A):

Re: instance and class-hierarchy ?

2006-03-29 Thread Rene Pijlman
Bror Johansson: >I have a class-hierarchy (fairly deep and fairly wide). > >Is there a good and general way to test an instance-object obj for having a >class belonging to a certain "sub-tree" of the hierarchy with a common >parent class C? isinstance(obj,C) -- René Pijlman -- http://mail.pytho

Re: instance references?

2006-01-31 Thread Scott David Daniels
Scott David Daniels wrote: > Alex Martelli wrote: (in effect) >>aptbase.drawables = weakref.WeakValueDictionary() >> then in each __init__ >>aptbase.drawables[len(aptbase.drawables)] = self >> then in show: >>for o in aptbase.drawables.values(): >># render it > > The keys you a

Re: instance references?

2006-01-30 Thread Alex Martelli
Scott David Daniels <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > My favourite way to use weakref is slightly different: I would have > >aptbase.drawables = weakref.WeakValueDictionary > typo here: > aptbase.drawables = weakref.WeakValueDictionary() > > then in each __init__ > >

Re: instance references?

2006-01-30 Thread Scott David Daniels
Alex Martelli wrote: > My favourite way to use weakref is slightly different: I would have >aptbase.drawables = weakref.WeakValueDictionary typo here: aptbase.drawables = weakref.WeakValueDictionary() > then in each __init__ >aptbase.drawables[len(aptbase.drawables)] = self > then in

  1   2   >