1

I would like create a class in python with method and sub-method.

Example what I want to do :

foo = Foo()
foo.playlist('my playlist').add('i_add_a_track_in_"my playlist".ogg')
foo.playlist('my playlist').delete('i_remove_this_track.ogg')

I have this code for now :

class Foo(object):
    def playlist(self, name):
        pass #my function...
    def add(self, track):
        pass #adding track
    def delete(self, track):
        pass #delete track

Please help me, I don't know how i can do it.

Thank you

3
  • 1
    en.wikipedia.org/wiki/Method_chaining Commented Aug 24, 2017 at 11:30
  • @MosesKoledoye I think we should choose another dupe that expresses the concept in your wikipedia link rather than shoehorning builtins. Commented Aug 24, 2017 at 11:37
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ Added Commented Aug 24, 2017 at 11:58

2 Answers 2

3

IIUC, you want to chain method calls one after another? All you'd have to do is return self at the end of each function.

class Foo(object):
    ...   
    def playlist(self, name):
        ...
        return self

    ... # and so on

MVCE:

In [229]: class Foo:
     ...:     def __init__(self, data):
     ...:         self.data = data
     ...:
     ...:     def add(self, val):
     ...:         self.data += val
     ...:         return self
     ...:
     ...:     def sub(self, val):
     ...:         self.data -= val
     ...:         return self
     ...:     

In [231]: x = Foo(0)

In [232]: x = x.add(10).sub(5) # or just x.add(10).sub(5)

In [233]: x.data
Out[233]: 5
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand correctly, foo.playlist('someplaylist').do_something() should actually be a shortcut for

playlist = foo('someplaylist')
playlist.do_something()

where playlist is NOT a foo object (ie: foo.do_something() is not supposed to make any sense and should just raise an error) but an instance of a distinct class.

If that's indeed the case, you actually want two classes: Foo with method playlist(...) that returns a Playlist object, and Playlist with add() and delete() methods:

class Playlist(object):
    def __init__(self, name):
        self.name = name

    def add(self, what):
        print("adding {} to playlist {}".format(what, self.name))

    def delete(self, what):
        print("deleting {} from playlist {}".format(what, self.name))


class Foo(object):
    def playlist(self, name):
        return Playlist(name)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.