Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use classes to hold data

using classes

How to use classes to hold data

by  Bong  Posted    (Edited  )
I'm sure this is a violation of canonical usage, but it serves to illustrate to the beginner (like me) how to use classes (and therefore objects) to encapsulate data.

Code:
#class test
class cA(object):
      a = 42
      def chA(self,b):
          self.a = 60
          return b

f=cA(); #  f instantiates the cA class
print f.a; #  the property 'a' of the object f will have
              the class's initial value, 42
x=f.chA(8); # the chA method of cA will change the 
              value and return the value you sent in.
print x; # that is, 8
print f.a; # however, now the 'a' property is 60
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top