1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonのメソッドでNoneType is not callable.

Posted at

バカな私はこんなクラスを書いて

class SessionManager:
    def __init__(self):
        self.continuous = 0
        self.stay_min = None
        self.datetime = None

    def stay_min(self, df: Series) -> float:
        """
        Args:
            df (Series): row including (user_id,
            datetime,continuos,session_id)

        Returns:
            
        """
        is_continuous: bool =\
            df.continuous - self.continuous == -1
        if is_continuous:
            dst = (self.datetime - df.datetime).total_seconds()/60
        else:
            dst = np.nan
            
        self.datetime = df.datetime
        self.continuous = df.continuous

        return dst
session_manager = SessionManager()
session_manager.stay_min()

としたのですが、NoneType is not callable.というエラーが出てしまい、20分どぶに捨てました。

なぜダメなのか

__init__(self)の中で、メソッドと同じ名前を定義しています。
そして、これはNoneで初期化されているので、当然関数として呼び出せません。

まとめ

名前空間に気をつけましょう・・・・

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?