Re: Question about classes

From: Steve Holden (steve_at_holdenweb.com)
Date: 11/22/04


Date: Mon, 22 Nov 2004 08:44:50 -0500

Steven Bethard wrote:

> Ben wrote:
>
>> class foo(xstart):
>> x = 5
>> if xstart != 0:
>> x = xstart
>>
>> a = foo(8)
>>
>> What I am curious about is why not? What am I missing about classes here?
>> Is the functionality delivered in some other fashion, or must I:
>>
>> class foo:
>> x = 5
>>
>> a = foo()
>> a.x = 8
>
>
> The parentheses after a class name do not indicate a parameter list;
> they indicate the list of base classes. So generally, they must by
> classes/types:
>
> >>> def make_class(base):
> .... class C(base):
> .... pass
> .... return C
> ....
> >>> make_class(object)
> <class '__main__.C'>
> >>> make_class(1)
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "<interactive input>", line 2, in make_class
> TypeError: Error when calling the metaclass bases
> int() takes at most 2 arguments (3 given)
>
> If you want to set a class or instance variable for an object at
> instantiation time, you probably want to supply the __init__ method:
>
> >>> class C(object):
> .... x = 5
> .... def __init__(self, x):
> .... if x != 0:
> .... C.x = x
> ....
> >>> c = C(0)
> >>> c.x
> 5
> >>> c = C(8)
> >>> c.x
> 8
>
> It seems strange to me that you want to set a *class* variable here, not
> an instance variable, though perhaps you have your reasons. Note that
> by doing so, every time you make a new instance, you'll change the x
> attribute for all objects:
>
> >>> C.x
> 5
> >>> c1 = C(0)
> >>> c1.x
> 5
> >>> C.x
> 5
> >>> c2 = C(8)
> >>> c2.x
> 8
> >>> c1.x
> 8
> >>> C.x
> 8
>
> If instead, you intended to set an instance variable, you might write it
> like:
>
> >>> class C(object):
> .... def __init__(self, x):
> .... if x != 0:
> .... self.x = x
> .... else:
> .... self.x = 5
> ....
> >>> c = C(0)
> >>> c.x
> 5
> >>> c = C(8)
> >>> c.x
> 8
>
> Steve

Don't forget, though, that due to the attribute resolution order, if an
instance doesn't have a particular attribute but the class does then a
reference to the attribute in a method will get the class attribute.

This has been used to implement class defaults in the past.

regards
  another Steve

-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119


Relevant Pages

  • RE: MISSING TEXT on web pages and email
    ... your Fonts are set too large thereby some of the text not showing fully on ... the page or it could be your security settings located in your System Folder ... "steve" wrote: ... >> I am having trouble with missing text when I go to web pages and emails. ...
    (microsoft.public.windowsxp.general)
  • RE: Help topics missing
    ... Since you have so many files/folders missing, ... "Veni, Vidi, Velcro" ... "Del" wrote: ... "Steve Sanford" wrote: ...
    (microsoft.public.access.modulesdaovba)
  • Re: Client Side - intercepting postback for form.
    ... Steve ... >> it formats some items and sticks them into hidden form fields. ... >> My goal is to have the page run a javascript function whenever the ... There must be something I'm missing because I ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Ball hopper Lister L
    ... > based member of this NG, Sunday steve and i traveled off down to ... > oxfordshire to collect quite a rare Lister L. There are a few important ... If anyone has any L type parts i'm missing that they would be ... > Regards Gary Millward ...
    (uk.rec.engines.stationary)
  • private field reflection of parent
    ... base classes that the type may have. ... hierarchy. ... Am I missing something? ... Prev by Date: ...
    (microsoft.public.dotnet.general)