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


Groups > comp.lang.python > #101905 > unrolled thread

Is this an attribute?

Started byRobert <rxjwg98@gmail.com>
First post2016-01-19 12:19 -0800
Last post2016-01-20 11:03 +1100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#101905 — Is this an attribute?

FromRobert <rxjwg98@gmail.com>
Date2016-01-19 12:19 -0800
SubjectIs this an attribute?
Message-ID<5e847125-7fce-49ce-96ab-25e6f3b5511b@googlegroups.com>
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?


Thanks,

[toc] | [next] | [standalone]


#101908

FromPeter Otten <__peter__@web.de>
Date2016-01-19 21:51 +0100
Message-ID<mailman.113.1453236703.15297.python-list@python.org>
In reply to#101905
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...

[toc] | [prev] | [next] | [standalone]


#101909

FromRobert <rxjwg98@gmail.com>
Date2016-01-19 13:27 -0800
Message-ID<81756faa-3b37-40e6-83f8-94af8fdfc3c4@googlegroups.com>
In reply to#101908
On Tuesday, January 19, 2016 at 3:52:12 PM UTC-5, Peter Otten wrote:
> 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...

I did not pay attention to the code you post. The attribute has been unclear
to me for sometime, especially for a child class. I just realize that class
attribute cannot be inherited to its child. 
Thanks

[toc] | [prev] | [next] | [standalone]


#101910

FromRobert <rxjwg98@gmail.com>
Date2016-01-19 13:49 -0800
Message-ID<40ac7601-5c22-4758-a11f-837c9ffa1997@googlegroups.com>
In reply to#101908
On Tuesday, January 19, 2016 at 3:52:12 PM UTC-5, Peter Otten wrote:
> 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...

Excuse me. My previous post was wrong on inheritance. 

That you point out that the out of __init__ initializer arguments, is
helpful for me. 

[toc] | [prev] | [next] | [standalone]


#101913

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-20 11:03 +1100
Message-ID<569ececc$0$1589$c3e8da3$5496439d@news.astraweb.com>
In reply to#101905
On Wed, 20 Jan 2016 07:19 am, Robert wrote:

> Hi,
> 
> When I read a code snippet below, I find I don't know what
> 'self.framelogprob' is on the child class.
[...]
> 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?


Just because you ask "give me attribute foo" doesn't mean attribute foo
exists. I can use attribute syntax to look up an attribute that doesn't
exist, and Python will raise AttributeError.


The question is, perhaps that attribute gets created elsewhere. You would
need to read all the source code, or read the documentation, to see where
and under what circumstances StubHMM objects gain a framelogprob attribute.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web