Stack implementation in python using jupyter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












This is my first program in Python: a stack implementation.



class Stack:
items =;
index =-1;

def isEmpty(self):
return self.index >=0;

def push(self,item):
self.index +=1;
print(self.index);
self.items.insert(self.index,item);


def pop(self):

if self.index<0:
raise Exception(" no items in list");
else:
del self.items[self.index];
self.index -=1;

def peek(self):
print(self.index);
if self.index >-1:
return self.items[self.index];



stackobj = Stack();
stackobj.push("solomon");
stackobj.push("helloworld")
stackobj.pop();

print(stackobj.peek())


Even though this program works I'm still confused as to whether I've written the program in the right way. I am also not sure when to use the self operator.







share|improve this question



























    up vote
    2
    down vote

    favorite












    This is my first program in Python: a stack implementation.



    class Stack:
    items =;
    index =-1;

    def isEmpty(self):
    return self.index >=0;

    def push(self,item):
    self.index +=1;
    print(self.index);
    self.items.insert(self.index,item);


    def pop(self):

    if self.index<0:
    raise Exception(" no items in list");
    else:
    del self.items[self.index];
    self.index -=1;

    def peek(self):
    print(self.index);
    if self.index >-1:
    return self.items[self.index];



    stackobj = Stack();
    stackobj.push("solomon");
    stackobj.push("helloworld")
    stackobj.pop();

    print(stackobj.peek())


    Even though this program works I'm still confused as to whether I've written the program in the right way. I am also not sure when to use the self operator.







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      This is my first program in Python: a stack implementation.



      class Stack:
      items =;
      index =-1;

      def isEmpty(self):
      return self.index >=0;

      def push(self,item):
      self.index +=1;
      print(self.index);
      self.items.insert(self.index,item);


      def pop(self):

      if self.index<0:
      raise Exception(" no items in list");
      else:
      del self.items[self.index];
      self.index -=1;

      def peek(self):
      print(self.index);
      if self.index >-1:
      return self.items[self.index];



      stackobj = Stack();
      stackobj.push("solomon");
      stackobj.push("helloworld")
      stackobj.pop();

      print(stackobj.peek())


      Even though this program works I'm still confused as to whether I've written the program in the right way. I am also not sure when to use the self operator.







      share|improve this question













      This is my first program in Python: a stack implementation.



      class Stack:
      items =;
      index =-1;

      def isEmpty(self):
      return self.index >=0;

      def push(self,item):
      self.index +=1;
      print(self.index);
      self.items.insert(self.index,item);


      def pop(self):

      if self.index<0:
      raise Exception(" no items in list");
      else:
      del self.items[self.index];
      self.index -=1;

      def peek(self):
      print(self.index);
      if self.index >-1:
      return self.items[self.index];



      stackobj = Stack();
      stackobj.push("solomon");
      stackobj.push("helloworld")
      stackobj.pop();

      print(stackobj.peek())


      Even though this program works I'm still confused as to whether I've written the program in the right way. I am also not sure when to use the self operator.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 19 at 17:12









      Billal BEGUERADJ

      1




      1









      asked Jan 6 at 9:41









      user3878073

      191




      191




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Welcome to the land of Python!



          If you come from Java, think as self being the same as this in Java.
          It is a reference to the object, the difference being in Python, you have to explicitly pass self as a parameter to the class methods.



          If you write in Java



          class Stack

          //constructor and class variables goes here

          void pop()
          if(this.index < 0)
          //logic of the function goes here




          In Python you write like you did here



          class Stack:
          # constructor and class variables goes here

          def pop(self):
          if self.index<0:
          # logic of the function goes here


          You see that self and this serve the same purpose.



          An another point, you should use documentation! As in PEP257 the format recommanded is like:



          def complex(real=0.0, imag=0.0):
          """Form a complex number.

          Keyword arguments:
          real -- the real part (default 0.0)
          imag -- the imaginary part (default 0.0)
          """
          if imag == 0.0 and real == 0.0:
          return complex_zero
          ...
          '''


          Another thing, to follow PEP8 it is better to write assignment and condition with a space after the operator.
          Write items = or self.index >= 0 instead of items = or self.index >=0.



          Also you can drop ; in Python, it's not Java :)



          Edit: Also look at @Jon's comments below






          share|improve this answer



















          • 1




            Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
            – Jon Clements
            Jan 6 at 14:06











          • Some very good points!
            – Julien Rousé
            Jan 6 at 14:17










          • (and .append instead of .insert) :)
            – Jon Clements
            Jan 6 at 14:20










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184429%2fstack-implementation-in-python-using-jupyter%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Welcome to the land of Python!



          If you come from Java, think as self being the same as this in Java.
          It is a reference to the object, the difference being in Python, you have to explicitly pass self as a parameter to the class methods.



          If you write in Java



          class Stack

          //constructor and class variables goes here

          void pop()
          if(this.index < 0)
          //logic of the function goes here




          In Python you write like you did here



          class Stack:
          # constructor and class variables goes here

          def pop(self):
          if self.index<0:
          # logic of the function goes here


          You see that self and this serve the same purpose.



          An another point, you should use documentation! As in PEP257 the format recommanded is like:



          def complex(real=0.0, imag=0.0):
          """Form a complex number.

          Keyword arguments:
          real -- the real part (default 0.0)
          imag -- the imaginary part (default 0.0)
          """
          if imag == 0.0 and real == 0.0:
          return complex_zero
          ...
          '''


          Another thing, to follow PEP8 it is better to write assignment and condition with a space after the operator.
          Write items = or self.index >= 0 instead of items = or self.index >=0.



          Also you can drop ; in Python, it's not Java :)



          Edit: Also look at @Jon's comments below






          share|improve this answer



















          • 1




            Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
            – Jon Clements
            Jan 6 at 14:06











          • Some very good points!
            – Julien Rousé
            Jan 6 at 14:17










          • (and .append instead of .insert) :)
            – Jon Clements
            Jan 6 at 14:20














          up vote
          1
          down vote













          Welcome to the land of Python!



          If you come from Java, think as self being the same as this in Java.
          It is a reference to the object, the difference being in Python, you have to explicitly pass self as a parameter to the class methods.



          If you write in Java



          class Stack

          //constructor and class variables goes here

          void pop()
          if(this.index < 0)
          //logic of the function goes here




          In Python you write like you did here



          class Stack:
          # constructor and class variables goes here

          def pop(self):
          if self.index<0:
          # logic of the function goes here


          You see that self and this serve the same purpose.



          An another point, you should use documentation! As in PEP257 the format recommanded is like:



          def complex(real=0.0, imag=0.0):
          """Form a complex number.

          Keyword arguments:
          real -- the real part (default 0.0)
          imag -- the imaginary part (default 0.0)
          """
          if imag == 0.0 and real == 0.0:
          return complex_zero
          ...
          '''


          Another thing, to follow PEP8 it is better to write assignment and condition with a space after the operator.
          Write items = or self.index >= 0 instead of items = or self.index >=0.



          Also you can drop ; in Python, it's not Java :)



          Edit: Also look at @Jon's comments below






          share|improve this answer



















          • 1




            Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
            – Jon Clements
            Jan 6 at 14:06











          • Some very good points!
            – Julien Rousé
            Jan 6 at 14:17










          • (and .append instead of .insert) :)
            – Jon Clements
            Jan 6 at 14:20












          up vote
          1
          down vote










          up vote
          1
          down vote









          Welcome to the land of Python!



          If you come from Java, think as self being the same as this in Java.
          It is a reference to the object, the difference being in Python, you have to explicitly pass self as a parameter to the class methods.



          If you write in Java



          class Stack

          //constructor and class variables goes here

          void pop()
          if(this.index < 0)
          //logic of the function goes here




          In Python you write like you did here



          class Stack:
          # constructor and class variables goes here

          def pop(self):
          if self.index<0:
          # logic of the function goes here


          You see that self and this serve the same purpose.



          An another point, you should use documentation! As in PEP257 the format recommanded is like:



          def complex(real=0.0, imag=0.0):
          """Form a complex number.

          Keyword arguments:
          real -- the real part (default 0.0)
          imag -- the imaginary part (default 0.0)
          """
          if imag == 0.0 and real == 0.0:
          return complex_zero
          ...
          '''


          Another thing, to follow PEP8 it is better to write assignment and condition with a space after the operator.
          Write items = or self.index >= 0 instead of items = or self.index >=0.



          Also you can drop ; in Python, it's not Java :)



          Edit: Also look at @Jon's comments below






          share|improve this answer















          Welcome to the land of Python!



          If you come from Java, think as self being the same as this in Java.
          It is a reference to the object, the difference being in Python, you have to explicitly pass self as a parameter to the class methods.



          If you write in Java



          class Stack

          //constructor and class variables goes here

          void pop()
          if(this.index < 0)
          //logic of the function goes here




          In Python you write like you did here



          class Stack:
          # constructor and class variables goes here

          def pop(self):
          if self.index<0:
          # logic of the function goes here


          You see that self and this serve the same purpose.



          An another point, you should use documentation! As in PEP257 the format recommanded is like:



          def complex(real=0.0, imag=0.0):
          """Form a complex number.

          Keyword arguments:
          real -- the real part (default 0.0)
          imag -- the imaginary part (default 0.0)
          """
          if imag == 0.0 and real == 0.0:
          return complex_zero
          ...
          '''


          Another thing, to follow PEP8 it is better to write assignment and condition with a space after the operator.
          Write items = or self.index >= 0 instead of items = or self.index >=0.



          Also you can drop ; in Python, it's not Java :)



          Edit: Also look at @Jon's comments below







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jan 20 at 15:50


























          answered Jan 6 at 13:58









          Julien Rousé

          446416




          446416







          • 1




            Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
            – Jon Clements
            Jan 6 at 14:06











          • Some very good points!
            – Julien Rousé
            Jan 6 at 14:17










          • (and .append instead of .insert) :)
            – Jon Clements
            Jan 6 at 14:20












          • 1




            Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
            – Jon Clements
            Jan 6 at 14:06











          • Some very good points!
            – Julien Rousé
            Jan 6 at 14:17










          • (and .append instead of .insert) :)
            – Jon Clements
            Jan 6 at 14:20







          1




          1




          Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
          – Jon Clements
          Jan 6 at 14:06





          Some other things, items should be an instance attribute and not a class one, index is redundant as you can do len(items) and then you're not manually carrying it about somewhere else, and the pop might as well use items.pop(0) and optionally catch the exception and raise a more meaningful one - raising Exception is fairly broad... peek also doesn't seem to mirror its behaviour and returns None. Also - if items is sufficient for an empty check - no need to check its length there.
          – Jon Clements
          Jan 6 at 14:06













          Some very good points!
          – Julien Rousé
          Jan 6 at 14:17




          Some very good points!
          – Julien Rousé
          Jan 6 at 14:17












          (and .append instead of .insert) :)
          – Jon Clements
          Jan 6 at 14:20




          (and .append instead of .insert) :)
          – Jon Clements
          Jan 6 at 14:20












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184429%2fstack-implementation-in-python-using-jupyter%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation