Newbie Tip: Classes and variable passing vs instances from ENGSOL 
Author Message
 Newbie Tip: Classes and variable passing vs instances from ENGSOL

I'm brand new to Python, and am reading all the books I can, as I'm sure the
other "newbies" are. As a result, I breadboard "stuff" to see how they work.

Seems a waste to to just let the scripts idle on my hard drive, so from time to
time I'll post them, with the hope they will be usefu to someonel. Bear in mind
I am using NT4, and Python21. No warrenty of course. <grin>

Here's the first, regarding classes/variables/instances:

class CLASS_4:

  def __init__(self_1, input):
      print "from the \"__init__ thing\"  :   " + input

  def get_foo_bar(self_2, value):
      self_2.foo_bar = value
      print "print from \"get_foo_bar\"   :   " + value

  def print_foo_bar(self_3):
      print "print from \"print_foo_bar\" :   " + self_3.foo_bar

""" Notice:
    While 'self' is the usual *name*, we can call it anything we like.
    Here I've used the name "self_1, self_2, self_3", to make the point.
    It's obvious that they are mutually exclusive in this case.
"""
# End CLASS_4

# Now run the program:

instance_1 = CLASS_4("input from instance_1 the first time")
instance_2 = CLASS_4("input from instance_2 the first time")

instance_1.get_foo_bar("hello from instance_1 after calling \"get_foo_bar\"")
instance_2.get_foo_bar("world from instance_2 after calling \"get_foo_bar\"")

instance_1.print_foo_bar()
instance_2.print_foo_bar()

# End module class_4.py

""" PROGRAM RESULTS:

from the "__init__ thing"  :   input from instance_1 the first time
from the "__init__ thing"  :   input from instance_2 the first time
print from "get_foo_bar"   :   hello from instance_1 after calling "get_foo_bar"
print from "get_foo_bar"   :   world from instance_2 after calling "get_foo_bar"
print from "print_foo_bar" :   hello from instance_1 after calling "get_foo_bar"
print from "print_foo_bar" :   world from instance_2 after calling "get_foo_bar"

"""



Mon, 19 Jan 2004 12:12:28 GMT  
 Newbie Tip: Classes and variable passing vs instances from ENGSOL

Quote:
>   def __init__(self_1, input):
>       print "from the \"__init__ thing\"  :   " + input

Just a brief tip: the above can exploit python's ability to use two quote
characters to delimit strings:

  def __init__(self_1, input):
      print 'from the "__init__ thing"  :   ' + input

also, since there's plenty of whitespace involved, you might as well use this
slightly faster form of the above print statement that doesn't involve a
string concatenation:

  def __init__(self_1, input):
      print 'from the "__init__ thing"  :  ', input

[note that I trimmed a space from the end of the first string to compensate
for the additional space incurred using the ','.

     Richard



Mon, 19 Jan 2004 12:32:32 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. NEWBIE TIP: Reading a file two different ways...from ENGSOL

2. NEWBIE TIP: Displaying the contents of an imported module...from ENGSOL

3. Class vs instance variables?

4. instances of classes and instances of instances

5. itcl and passing class instances

6. Newbie question: Logical variable in CLP/Prolog vs variable in C

7. Class vs Instance methods

8. Creating a instance variable with a new class.

9. Class-instance variables

10. Class instance variables

11. Class vs Instance methods

12. class instance variable question

 

 
Powered by phpBB® Forum Software