Help with Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Loismustdie129
    New Member
    • Aug 2006
    • 194

    Help with Classes

    This may seem like a basic question but I still don't understand classes, I have tried all the tutorials. I tried to build my own class to return the python version and platform version, but it failed. If you guys could look at it and tell me what I am doing wrong, I think I can make better programs for python.

    Here is the code:

    Code:
    >>>  import sys, os
    >>>  class TestClass:
              "This class will return your python version and platform."
               def version():
                  return sys.version
               def platform():
                  return sys.platform
    And here is the output:

    Code:
    >>>  TestClass.version
    <unbound method TestClass.version>
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Loismustdie129
    This may seem like a basic question but I still don't understand classes, I have tried all the tutorials. I tried to build my own class to return the python version and platform version, but it failed. If you guys could look at it and tell me what I am doing wrong, I think I can make better programs for python.

    Here is the code:

    Code:
    >>>  import sys, os
    >>>  class TestClass:
              "This class will return your python version and platform."
               def version():
                  return sys.version
               def platform():
                  return sys.platform
    And here is the output:

    Code:
    >>>  TestClass.version
    <unbound method TestClass.version>
    You need to make an instance (live object in memory) of a class in order to use it. It also looks like you forgot the parens. that make a fuction call:
    Code:
    testMyClass = TestClass()
    testMyClass.version()

    Comment

    • Loismustdie129
      New Member
      • Aug 2006
      • 194

      #3
      Originally posted by bartonc
      You need to make an instance (live object in memory) of a class in order to use it. It also looks like you forgot the parens. that make a fuction call:
      Code:
      testMyClass = TestClass()
      testMyClass.version()
      I tried what you said, I tried to make an instance but it still didn't work. Maybe I don't know what an instance is but after I fixed it I got this output of testMyClass.ver sion:

      Code:
      <bound method TestClass.version of <__main__.TestClass instance at 0x00C5CFD0>>

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Loismustdie129
        I tried what you said, I tried to make an instance but it still didn't work. Maybe I don't know what an instance is but after I fixed it I got this output of testMyClass.ver sion:

        Code:
        <bound method TestClass.version of <__main__.TestClass instance at 0x00C5CFD0>>
        You're still not using "()" as in my example. But now you can see the difference between a bound and an unbound method. The latter comes from an uninstanciated class object. The syntax "testMyClass.ve rsion" merely asks the python shell to print the repr() of that object and would do nothing in a program. The parens are required to "call" the function (method): testMyClass.ver sion()

        Comment

        • Loismustdie129
          New Member
          • Aug 2006
          • 194

          #5
          Thanks Barton, I am getting this now.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by Loismustdie129
            This may seem like a basic question but I still don't understand classes, I have tried all the tutorials. I tried to build my own class to return the python version and platform version, but it failed. If you guys could look at it and tell me what I am doing wrong, I think I can make better programs for python.

            Here is the code:

            Code:
            >>>  import sys, os
            >>>  class TestClass:
                      "This class will return your python version and platform."
                       def version():
                          return sys.version
                       def platform():
                          return sys.platform
            And here is the output:

            Code:
            >>>  TestClass.version
            <unbound method TestClass.version>
            Define your class. The definition does not create an instance. Create an instance by calling a class object as a function (include arguments if required by the object). This creates an instance by calling method '__new__()' (this is transparent to the user), which, in turn, calls class method '__init__()' if defined. Instance attributes are then available and methods can be called. Methods will receive the argument 'self' automatically, so it must be present in the argument list. If you want the instance to be displayed a certain way by 'print', create special method '__repr__'. There are several other special methods - some are defined automatically and some you can define in your class. Example:
            Code:
            import sys, os
            class TestClass(object):
                "This class will return your python version and platform."
                def __init__(self, s):
                    self.data = list(s)
                def version(self):
                  return sys.version
                def platform(self):
                  return sys.platform
                def __repr__(self):
                    return "You have created an instance of 'TestClass'"
            
            a = TestClass('create a list')
            print a
            print a.data
            print a.version()
            print a.platform()
            Interactive Pythonwin:
            Code:
            >>> You have created an instance of 'TestClass'
            ['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']
            2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]
            win32
            >>> dir(a)
            ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'data', 'platform', 'version']
            >>> print a.__doc__
            This class will return your python version and platform.
            >>> print a.__class__
            <class '__main__.TestClass'>
            >>> for key in a.__dict__.keys():
            ... 	print 'instance.%s = %s' % (key, a.__dict__[key])
            ... 	
            instance.data = ['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']
            >>>
            HTH :)
            BV

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by Loismustdie129
              Thanks Barton, I am getting this now.
              You're welcome. Don't miss this thread.

              Comment

              • Loismustdie129
                New Member
                • Aug 2006
                • 194

                #8
                Originally posted by bvdet
                Define your class. The definition does not create an instance. Create an instance by calling a class object as a function (include arguments if required by the object). This creates an instance by calling method '__new__()' (this is transparent to the user), which, in turn, calls class method '__init__()' if defined. Instance attributes are then available and methods can be called. Methods will receive the argument 'self' automatically, so it must be present in the argument list. If you want the instance to be displayed a certain way by 'print', create special method '__repr__'. There are several other special methods - some are defined automatically and some you can define in your class. Example:
                Code:
                import sys, os
                class TestClass(object):
                    "This class will return your python version and platform."
                    def __init__(self, s):
                        self.data = list(s)
                    def version(self):
                      return sys.version
                    def platform(self):
                      return sys.platform
                    def __repr__(self):
                        return "You have created an instance of 'TestClass'"
                
                a = TestClass('create a list')
                print a
                print a.data
                print a.version()
                print a.platform()
                Interactive Pythonwin:
                Code:
                >>> You have created an instance of 'TestClass'
                ['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']
                2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]
                win32
                >>> dir(a)
                ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'data', 'platform', 'version']
                >>> print a.__doc__
                This class will return your python version and platform.
                >>> print a.__class__
                <class '__main__.TestClass'>
                >>> for key in a.__dict__.keys():
                ... 	print 'instance.%s = %s' % (key, a.__dict__[key])
                ... 	
                instance.data = ['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']
                >>>
                HTH :)
                BV
                Well thank you, that was the last part I needed, with the __init__ function. I just thought that I was doing something else wrong and I would change that later. But I guess I did that wrong too :-).

                Comment

                Working...