Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101908

Re: Is this an attribute?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Is this an attribute?
Date Tue, 19 Jan 2016 21:51:27 +0100
Organization None
Lines 74
Message-ID <mailman.113.1453236703.15297.python-list@python.org> (permalink)
References <5e847125-7fce-49ce-96ab-25e6f3b5511b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de ALxlitUR9roxxy7lfBzJnwG/OivYsJp3YZD+VuS5diow==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'attribute.': 0.09; 'definition,': 0.09; 'grep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'python': 0.10; 'def': 0.13; 'arbitrarily': 0.16; 'attributes.': 0.16; 'dot,': 0.16; 'last)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'text?': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'attribute': 0.18; 'recognize': 0.22; 'bit': 0.23; '(most': 0.24; 'module': 0.25; 'testing': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'idea': 0.28; "skip:' 10": 0.28; 'arguments,': 0.29; 'sentence': 0.29; 'code': 0.30; 'class.': 0.30; "i'd": 0.31; 'supposed': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'says': 0.32; 'run': 0.33; 'class': 0.33; 'traceback': 0.33; 'skip:- 10': 0.34; 'file': 0.34; 'add': 0.34; 'that,': 0.34; 'robert': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'tool': 0.36; 'web,': 0.36; 'child': 0.36; "wasn't": 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:org': 0.37; 'things': 0.38; 'wrong': 0.38; 'skip:p 20': 0.38; 'hi,': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'complete': 0.63; 'picked': 0.66; 'therefore': 0.67; 'book,': 0.72; 'widen': 0.84; 'x):': 0.84; 'subject:this': 0.85
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8595.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101908

Show key headers only | View raw


Robert wrote:

> Hi,
> 
> When I read a code snippet below, I find I don't know what
> 'self.framelogprob' is on the child class.
> 
> 
> 
> ////// parent class
> class _BaseHMM(BaseEstimator):
>     def __init__(self, n_components=1,
>                  startprob_prior=1.0, transmat_prior=1.0,
>                  algorithm="viterbi", random_state=None,
>                  n_iter=10, tol=1e-2, verbose=False,
>                  params=string.ascii_letters,
>                  init_params=string.ascii_letters):
>         self.n_components = n_components
> ......
> 
>     def score_samples(self, X, lengths=None):
>         X = check_array(X)
>         n_samples = X.shape[0]
>         logprob = 0
>         posteriors = np.zeros((n_samples, self.n_components))
>         for i, j in iter_from_X_lengths(X, lengths):
>             framelogprob = self._compute_log_likelihood(X[i:j])
> .......
>         return logprob, posteriors
> 
> ////// child class
> class StubHMM(_BaseHMM):
>     def _compute_log_likelihood(self, X):
>         return self.framelogprob
> -------------
> 
> On Python web, it says that things after dot, such as a class name, are
> attributes. From this definition, 'framelogprob' is an attribute. But when
> I run the command on Canopy:
> 
> h.framelogprob
> 
---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call
> last) <ipython-input-19-970ee2f3402c> in <module>()
> ----> 1 h.framelogprob
> 
> AttributeError: 'StubHMM' object has no attribute 'framelogprob'
> 
> 
> it doesn't recognize it as an attribute. What is wrong with my
> understanding?

When you are reading a book, do you expect to be able to understand every 
arbitrarily picked sentence without any idea about the surrounding text?

Widen your view a bit -- the StubHMM class is in a file test_base.py and 
therefore it is likely that it is supposed to help with testing rather than 
to be used standalone. When you read the complete module to understand more 
of the context or just use a tool like grep you'll find the following 
snippets:

        h = StubHMM(2)
        h.transmat_ = [[0.7, 0.3], [0.3, 0.7]]
        h.startprob_ = [0.5, 0.5]
        h.framelogprob = self.framelogprob

        h = StubHMM(n_components)
        h.framelogprob = self.framelogprob

So the attribute is set "from the outside". I wouldn't do that, I'd rather 
add initializer arguments, but that wasn't the question...

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this an attribute? Robert <rxjwg98@gmail.com> - 2016-01-19 12:19 -0800
  Re: Is this an attribute? Peter Otten <__peter__@web.de> - 2016-01-19 21:51 +0100
    Re: Is this an attribute? Robert <rxjwg98@gmail.com> - 2016-01-19 13:27 -0800
    Re: Is this an attribute? Robert <rxjwg98@gmail.com> - 2016-01-19 13:49 -0800
  Re: Is this an attribute? Steven D'Aprano <steve@pearwood.info> - 2016-01-20 11:03 +1100

csiph-web