What is self.file = file for?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wxPythoner@gmail.com

    What is self.file = file for?

    Hello!

    I have trouble understanding something in this code snippet:

    class TextReader:
    """Print and number lines in a text file."""
    def __init__(self, file):
    self.file = file
  • Gary Herron

    #2
    Re: What is self.file = file for?

    wxPythoner@gmai l.com wrote:
    Hello!
    >
    I have trouble understanding something in this code snippet:
    >
    class TextReader:
    """Print and number lines in a text file."""
    def __init__(self, file):
    self.file = file
    .
    .
    .
    >
    >
    When would you do a thing like self.file = file ? I really don't
    find an answer on this. Please help me understand this.
    --

    >
    If you know about Object-Oriented Programming this should make sense.
    If you don't, then you have some reading/learning to do first.

    When someone wants to create an object of type TextReader, they must
    supply a value
    ob = TextReader(v)
    That calls the __init__ constructor with the supplied value of v in the
    variable named file.
    If the object being created wants to record the value for future use,
    then the line
    self.file = file
    does just that. "self" is the name of the object being created,
    "self.file" is an attribute named "file" of that object, and the
    assignment stores the supplied value there.

    Gary Herron


    Comment

    • Daniel Fetchinson

      #3
      Re: What is self.file = file for?

      I have trouble understanding something in this code snippet:
      >
      class TextReader:
      """Print and number lines in a text file."""
      def __init__(self, file):
      self.file = file
      .
      .
      .
      >
      >
      When would you do a thing like self.file = file ? I really don't
      find an answer on this. Please help me understand this.
      This is a standard object oriented programming idiom. You might find
      it useful to ask around on the 'tutor' mailing list of python --
      http://mail.python.org/mailman/listinfo/tutor -- where you'll get
      detailed explanations on basic OOP and python topics.

      Cheers,
      Daniel
      --
      Psss, psss, put it down! - http://www.cafepress.com/putitdown

      Comment

      • afrobeard

        #4
        Re: What is self.file = file for?

        If you are familiar to C++ or a similar language, the concept of the
        this pointer might not be alien to you. self in this context is
        basically a reference to the class itself. Hence self.file is creating
        a class member and setting to the input from file.

        As Gary pointed out, during initialization, only the latter parameter
        i.e. file is being passed to __init__

        You can get a brief tutorial from http://docs.python.org/tut/node11.html
        about classes in python.

        On May 14, 3:08 am, wxPytho...@gmai l.com wrote:
        Hello!
        >
        I have trouble understanding something in this code snippet:
        >
        class TextReader:
            """Print and number lines in a text file."""
            def __init__(self, file):
                self.file = file
                .
                .
                .
        >
        When would you do a thing like  self.file = file  ? I really don't
        find an answer on this. Please help me understand this.

        Comment

        • Gary Herron

          #5
          Re: What is self.file = file for?

          Chester wrote:
          I see. A very good explanation indeed. Thank you for it. But why would
          anyone want to do this anyway?
          >
          In a single sentence, OOP (Object Oriented Programming) is a way to
          organize a program around your data and the operations on that data.
          Many books and college courses are dedicated to OOP. Years could be
          spent learning to apply its concepts. A web search would find a flood
          of such information.

          Gary Herron
          >
          >
          >
          On Wed, May 14, 2008 at 12:25 AM, Gary Herron
          <gherron@island training.comwro te:
          >
          >wxPythoner@gmai l.com wrote:
          >>
          >>
          >>Hello!
          >>>
          >>I have trouble understanding something in this code snippet:
          >>>
          >>class TextReader:
          >> """Print and number lines in a text file."""
          >> def __init__(self, file):
          >> self.file = file
          >> .
          >> .
          >> .
          >>>
          >>>
          >>When would you do a thing like self.file = file ? I really don't
          >>find an answer on this. Please help me understand this.
          >>--
          >>http://mail.python.org/mailman/listinfo/python-list
          >>>
          >>>
          >>>
          > If you know about Object-Oriented Programming this should make sense. If
          >you don't, then you have some reading/learning to do first.
          >>
          > When someone wants to create an object of type TextReader, they must supply
          >a value
          > ob = TextReader(v)
          > That calls the __init__ constructor with the supplied value of v in the
          >variable named file.
          > If the object being created wants to record the value for future use, then
          >the line
          > self.file = file
          > does just that. "self" is the name of the object being created,
          >"self.file" is an attribute named "file" of that object, and the assignment
          >stores the supplied value there.
          >>
          > Gary Herron
          >>
          >>
          >>
          >>

          Comment

          • Bruno Desthuilliers

            #6
            Re: What is self.file = file for?

            afrobeard a écrit :

            (top-post corrected. Please, do not top-post).
            On May 14, 3:08 am, wxPytho...@gmai l.com wrote:
            >Hello!
            >>
            >I have trouble understanding something in this code snippet:
            >>
            >class TextReader:
            > """Print and number lines in a text file."""
            > def __init__(self, file):
            > self.file = file
            > .
            > .
            > .
            >>
            >When would you do a thing like self.file = file ? I really don't
            >find an answer on this. Please help me understand this.
            >
            If you are familiar to C++ or a similar language, the concept of the
            this pointer might not be alien to you. self in this context is
            basically a reference to the class itself.
            Nope. It's a reference to the instance.
            Hence self.file is creating
            a class member
            Nope. It's setting an instance attribute.
            and setting to the input from file.
            >
            As Gary pointed out, during initialization, only the latter parameter
            i.e. file is being passed to __init__
            Nope. Obviously, both parameters are passed - else it just wouldn't
            work. Given an object 'obj' instance of class 'Cls', you can think of
            obj.method(arg) as a convenient shortcut for Cls.method(obj, arg).

            Comment

            • castironpi@gmail.com

              #7
              Re: What is self.file = file for?

              On May 14, 2:26 am, Bruno Desthuilliers <bruno.
              42.desthuilli.. .@websiteburo.i nvalidwrote:
              afrobeard a écrit :
              >
              (top-post corrected. Please, do not top-post).
              >
              >
              >
              >
              >
              On May 14, 3:08 am, wxPytho...@gmai l.com wrote:
              Hello!
              >
              I have trouble understanding something in this code snippet:
              >
              class TextReader:
                  """Print and number lines in a text file."""
                  def __init__(self, file):
                      self.file = file
                      .
                      .
                      .
              >
              When would you do a thing like  self.file = file  ? I really don't
              find an answer on this. Please help me understand this.
              >
              If you are familiar to C++ or a similar language, the concept of the
              this pointer might not be alien to you. self in this context is
              basically a reference to the class itself.
              >
              Nope. It's a reference to the instance.
              >
              Hence self.file is creating
              a class member
              >
              Nope. It's setting an instance attribute.
              >
              and setting to the input from file.
              >
              As Gary pointed out, during initialization, only the latter parameter
              i.e. file is being passed to __init__
              >
              Nope. Obviously, both parameters are passed - else it just wouldn't
              work. Given an object 'obj' instance of class 'Cls', you can think of
              obj.method(arg) as a convenient shortcut for Cls.method(obj, arg).- Hide quoted text -
              >
              - Show quoted text -
              I am at the point of open-source, and I agree.

              Comment

              Working...