7 Replies - 2726 Views - Last Post: 04 April 2011 - 06:50 AM Rate Topic: -----

#1 e-papa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 17-March 11

understanding classes in python

Posted 19 March 2011 - 02:45 AM

I'm new to python, but I being doing well so far, I happen to be a little held back at Classes and objects, some things I see in books are not working with my version of Python, portable python v1.1py3.0.1
class Point:
    """represents a point in 2-D space"""
blank=Point()
blank.x=3.0
blank.y=4.0
print (%g, %g) % (blank.x, blank.y)

the print part of the code does not seem to work, I know in python 3.x the print function is now written print() but I've tried adapting it to python3.x but it keeps coming up with an error saying invalid character in identifier.
Please put me through.

Is This A Good Question/Topic? 0
  • +

Replies To: understanding classes in python

#2 prajayshetty  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: understanding classes in python

Posted 19 March 2011 - 07:30 AM

if ur class is python 3.1 +++ greater then do it like this
class Point:
    x=0
    y=0
   

def main():
   blank=Point()
   blank.x=3.0
   blank.y=4.0
   print(blank.x,blank.y)
   return

main()



this should work on python 3.0 +++ actually there is lot of changes between 2.7 and 3.0 so i guess u might be reading code from 2.7 and writting on a 3.0 python compiler so just check it

This post has been edited by prajayshetty: 19 March 2011 - 07:31 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,667
  • Joined: 23-August 08

Re: understanding classes in python

Posted 19 March 2011 - 07:34 AM

print (%g, %g) % (blank.x, blank.y)


Your quotes look like "smart quotes" from a word processor. What are you using to create your file?
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: understanding classes in python

Posted 19 March 2011 - 08:03 AM

View Postprajayshetty, on 19 March 2011 - 10:30 AM, said:

if ur class is python 3.1 +++ greater then do it like this
class Point:
    x=0
    y=0
   

def main():
   blank=Point()
   blank.x=3.0
   blank.y=4.0
   print(blank.x,blank.y)
   return

main()



this should work on python 3.0 +++ actually there is lot of changes between 2.7 and 3.0 so i guess u might be reading code from 2.7 and writting on a 3.0 python compiler so just check it



Not to be a jerk, but there are a couple of problems here.

First off, 2.x and 3.x declare classes in the same way, the only change relevant to this example is with printing.

Secondly, this:

class Point:
    x=0
    y=0



is actually poor form. Despite it being allowed and even being displayed on Python.org (twitch), it's bad. The biggest problem occurs if one of those values were mutable. You can learn more about the default mutable value glitch feature here.

A better form would be:
class Point:
    def __init__(self,x=0,y=0):
        self.x=x
        self.y=y


The added benefit is that Point is now parameterized so that you can define any point in 1 statement instead of up to 3.

Thirdly, throwing main in at the end is nice for testing, but what if he wanted to use the Point class as a module for a bigger project? It would run the main function on import. That's no good. We can easily fix that by changing the end to:

if __name__ == "__main__":
    main()


This makes it so that main will not run when it's imported as a piece of a bigger project, but WILL run when it's ran in a stand-alone setting.

Fourthly, while this is a minor critique, the return statement on line 11 is meaningless. If you follow the convention that every function must end with a return statement, there's no harm in keeping it, but it adds nothing.

Fifthly, using a __str__ function for the printing adds some simplicity. An example of this is at the bottom.

Lastly, cPython is an interpreted language, not a compiled one. Thus there are differences between the 2.7 and 3.1 interpreters... again, this is a minor critique.


Here are all the updates I recommend for the code:
class Point:
    def __init__(self,x=0,y=0):
        self.x=x
        self.y=y

    def __str__(self):
        return "("+str(self.x)+","+str(self.y)+")"

def main(x,y):
   blank=Point(x,y)
   print(blank)

if __name__ == "__main__":
    main(3,4)



EDIT:
e-papa please don't down rep JackOfAllTrades just because you don't like his question. He's absolutely right, smartquotes can will screw up your code if they're in it.

This post has been edited by atraub: 21 March 2011 - 06:44 AM

Was This Post Helpful? 2
  • +
  • -

#5 sublimeike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-October 09

Re: understanding classes in python

Posted 01 April 2011 - 08:28 PM

atraub, you lost me at "thirdly" and "fifthly". Can't speak for everyone, but you may have just flew over some "new to python" users heads.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,667
  • Joined: 23-August 08

Re: understanding classes in python

Posted 02 April 2011 - 05:50 AM

The thirdly is simply that having main() at the end locks the class to being used only in this program. With atraub's change, the file can also be imported as a module into another program. For example, taking his change, you can then create another script which uses it, like this:

#!/usr/bin/env python                                                           

import point

square = []
square.append(point.Point(0, 0))
square.append(point.Point(2, 0))
square.append(point.Point(2, 2))
square.append(point.Point(0, 2))

for vertex in square:
    print(vertex)



which can then be run to yield

./square.py 
(0,0)
(2,0)
(2,2)
(0,2)



or the original script itself could be run to yield

./point.py 
(3,4)



As to the fifthly, ever Python object has a __str__ method which can be overridden to provide a nicely printed string, so that when print is called with an object, the object's value is printed as desired; in this case as a coordinate pair, as usually seen with Cartesian coordinates.
Was This Post Helpful? 1
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: understanding classes in python

Posted 03 April 2011 - 02:29 PM

JackOfAllTrades explained my explanation better than I could have explained it!:)

This post has been edited by atraub: 03 April 2011 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 e-papa  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 45
  • Joined: 17-March 11

Re: understanding classes in python

Posted 04 April 2011 - 06:50 AM

Thanks guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1