Setting Class Attributes

Steve Holden steve at holdenweb.com
Wed Oct 26 00:03:40 EDT 2005


the.theorist wrote:
> So that it'll be easier to remember the next time I find myself in the
> same situation on a different task, I'll extend the discussion
> somewhat.
> 
> Coming from C, I had expected that I'd get a new empty dict every time
> the __init__ function ran. Guido (or some other benevolent) had decided
> to implement things a little bit differently in Python. I understand
> that most everything is a pointer in Python. (which gives us cool stuff
> like recursive data structures) So I was wondering, they could have
> made the behavior C-like, but chose not to. The decision to bind
> everything in the function to the same default args must be a
> reflection of the Python Ideology. Philosophically, why was it done
> this way, and how does it fit in with Python's design as a language.
> 
I couldn't claim to be channelling the developers, but I suspect that 
the decision was pragmatic.

Consider what would have to be done in the case of a method prototype 
such as

class myClass(object):
     def __init__(arg1, arg2=default):
         print "Arg2 is:", arg2

Remember as well that the "def" statement is executable: when executed 
it binds the function name to the compiled function body in the current 
namespace.

Suppose the "default" name had gone out of scope by the time the 
instances are created: what value would you have the ionterpreter bind 
to the argument in that case?

> (hopefully, reasons will help me remeber why things are the way they
> are, so I don't forget in the future)
> -----------------
> I've only been using Python for a few weeks. (Chose it over Perl,
> because Python syntax is cleaner). I really like Python (over C), as it
> makes coding and debugging much faster and easier.
> 
Yup, it's a great languae, and it seems to be increasing in popularity 
quite rapidly.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list