Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please note that </p> <pre><code>class Foo(object): a = None </code></pre> <p>sets a key-value pair in <code>Foo</code>'s dict:</p> <pre><code>Foo.__dict__['a']=None </code></pre> <p>while</p> <pre><code>def __init__(self, a = None, b = None, c = None): self.a = a </code></pre> <p>sets a key-value pair in the Foo instance object's dict:</p> <pre><code>foo=Foo() foo.__dict__['a']=a </code></pre> <p>So setting the class members at the top of your definition is not directly related to the setting of the instance attributes in the lower half of your definition (inside the <code>__init__</code>.</p> <p>Also, it is good to be aware that <code>__init__</code> is Python's <em>initializer</em>. <code>__new__</code> is the class <em>constructor</em>.</p> <hr> <p>If you are looking for a way to automatically add some instance attributes based on <code>__init__</code>'s arguments, you could use this:</p> <pre><code>import inspect import functools def autoargs(*include,**kwargs): def _autoargs(func): attrs,varargs,varkw,defaults=inspect.getargspec(func) def sieve(attr): if kwargs and attr in kwargs['exclude']: return False if not include or attr in include: return True else: return False @functools.wraps(func) def wrapper(self,*args,**kwargs): # handle default values for attr,val in zip(reversed(attrs),reversed(defaults)): if sieve(attr): setattr(self, attr, val) # handle positional arguments positional_attrs=attrs[1:] for attr,val in zip(positional_attrs,args): if sieve(attr): setattr(self, attr, val) # handle varargs if varargs: remaining_args=args[len(positional_attrs):] if sieve(varargs): setattr(self, varargs, remaining_args) # handle varkw if kwargs: for attr,val in kwargs.iteritems(): if sieve(attr): setattr(self,attr,val) return func(self,*args,**kwargs) return wrapper return _autoargs </code></pre> <p>So when you say</p> <pre><code>class Foo(object): @autoargs() def __init__(self,x,path,debug=False,*args,**kw): pass foo=Foo('bar','/tmp',True, 100, 101,verbose=True) </code></pre> <p>you automatically get these instance attributes:</p> <pre><code>print(foo.x) # bar print(foo.path) # /tmp print(foo.debug) # True print(foo.args) # (100, 101) print(foo.verbose) # True </code></pre> <p>PS. Although I wrote this (for fun), I don't recommend using <code>autoargs</code> for serious work. Being explicit is simple, clear and infallible. I can't say the same for <code>autoargs</code>.</p> <p><strike>PPS. Is it just me, or are a lot of buttons broken on Stackoverflow? The editor window has lost all its icons... :(</strike> Clearing the browser cache fixed the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload