This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Scripting 101

Class log Class 13

hemanth hm's picture
Sat, 2010-10-30 19:03

* hemanth has changed the topic to: PYTHON OOP
<hemanth> class ClassName:
<hemanth>     <statement-1>
<hemanth>     .
<hemanth>     .
<hemanth>     .
<hemanth>     <statement-N>
<hemanth> is the skeleton of py class
<hemanth> obj1 = ClassName()
<hemanth> that would create an object of that class
<hemanth> so lets see an example that the best way things work
<hemanth> class MyClass:
<hemanth>     """A simple example class"""
<hemanth>     i = 12345
<hemanth>     def f(self):
<hemanth>         return 'hello world'
<hemanth> the example is from the docs of py
<hemanth> SourRust, PaBLoX are you getting my msg, there was some n/w issue here
* Now talking on #p2pu-webcraft/scripting-101
* Topic for #p2pu-webcraft/scripting-101 is: PYTHON OOP
* Topic for #p2pu-webcraft/scripting-101 set by hemanth!~hemanth@122.167.182.245 at Sat Oct 30 21:33:08 2010
<PaBLoX> ??
<PaBLoX> im still here D:
<SourRust_> nothing
<PaBLoX> I'm surrounded by ghosts :P
<hemanth_> SourRust_, PaBLoX getting my msgs there was some n/w issue
<SourRust_> i lost my connection
<hemanth_> Halloween started already is it?
<hemanth_> :D
<hemanth_> hmm, so that class example i gave was from the docs
<hemanth_> as the CheckBook example will be implemented by you guys :)
<hemanth_> so, myobj = MyClass
<hemanth_> will create an instance of the class called MyClass
<hemanth_> to access the variables and methods of the class
<hemanth_> we use the "dot" operator
<hemanth_> like myobj.i=420
<hemanth_> would set the value of the attribute called i from the class MyClass to 420 from 12345
<hemanth_> myobj.f()
<hemanth_> would invoke the method f() in the class MyClass
<hemanth_> any o/ ?
* Topic for #p2pu-webcraft/scripting-101 is: PYTHON OOP
* Topic for #p2pu-webcraft/scripting-101 set by hemanth!~hemanth@122.167.182.245 at Sat Oct 30 21:33:08 2010
* Notify: FRockstock is online (Ubuntu Servers).
<hemanth> PaBLoX, what was the last msg u got?
<PaBLoX> what's going on?
<PaBLoX> D:
<hemanth> :D
<PaBLoX> rerturn 'hello world'
<hemanth> both me and SourRust are having a n/w
<hemanth> issue
<PaBLoX> and then some discussion
<hemanth> he he ok
<PaBLoX> about halloween and ghosts =P
<hemanth> indeed!
<hemanth> :)
<hemanth> So, whoz that playing the prank on us?!
<hemanth> the example is from the docs of py
<hemanth> SourRust, PaBLoX are you getting my msg, there was some n/w issue here
<hemanth> hmm, so that class example i gave was from the docs
<hemanth> as the CheckBook example will be implemented by you guys :)
<hemanth> so, myobj = MyClass
<hemanth> will create an instance of the class called MyClass
<hemanth>  to access the variables and methods of the class
<hemanth> we use the "dot" operator
<hemanth> like myobj.i=420
<hemanth> would set the value of the attribute called i from the class MyClass to 420 from 12345
<hemanth> myobj.f()
<hemanth> would invoke the method f() in the class MyClass
<hemanth> any o/ ?
* hemanth msgs so far, that might be missed
<PaBLoX> so the attributes can be modified "on the fly"
<PaBLoX> I imagine that a method could contain that attribute, right?
<hemanth> yes, for this example
<hemanth> yes indeed
<hemanth> method could contain that attribute, so to access them...
<hemanth> we use self.<attr_name>
<hemanth> that is self.i
<hemanth> in the example
<hemanth> where self, means the attribute must be of that current object that invoked the method
* SourRust (473b12d5@gateway/web/freenode/ip.71.59.18.213) has joined #p2pu-webcraft/scripting-101
* hemanth send the missed logs due to n/w issues to SourRust
<PaBLoX> class AnotherClass:
<PaBLoX>     i = 2
<PaBLoX>     def double(self):
<PaBLoX>         return self.i * 2
<PaBLoX> obj2 = AnotherClass()
<PaBLoX> In [10]: obj2.i
<PaBLoX> Out[10]: 2
<PaBLoX> obj2.double()
<PaBLoX> Out[13]: 4
<PaBLoX> obj2.i = 4
<PaBLoX> obj2.double()
<PaBLoX> Out[15]: 8
<PaBLoX> That's the correct way to use an attribute inside a method then?
<PaBLoX> self.i
<PaBLoX> ?
<hemanth> yes
<hemanth> one of the ways
* hemanth back in a min
<SourRust> kk all caught up
<PaBLoX> Sorry, i'll have to leave now
<PaBLoX> It's 13:30
<PaBLoX> (here)
<PaBLoX> So it's lunch time and we're in a bbq
<PaBLoX> I'll catch with the logs, tomorrow I'll be at time :)
<PaBLoX> Regards!
<SourRust> cya
<PaBLoX> *on time
<PaBLoX> (My english teacher would kill me!)
<hemanth> PaBLoX, SourRust Sorry me back
<hemanth> cable guy was here
* SourRust_ (473b12d5@gateway/web/freenode/ip.71.59.18.213) has joined #p2pu-webcraft/scripting-101
<hemanth> so that was about basic py method invocation and attribute set/reset by an object
<hemanth> class DerivedClassName(BaseClassName):
<hemanth>     <statement-1>
<hemanth>     .
<hemanth>     .
<hemanth>     .
<hemanth>     <statement-N>
<hemanth> is how inheritance is possible
* SourRust has quit (Ping timeout: 265 seconds)
<hemanth> that is the parent child relationship talked b4
<hemanth> class DerivedClassName(Base1, Base2, Base3):
<hemanth>     <statement-1>
<hemanth>     .
<hemanth>     .
<hemanth>     .
<hemanth>     <statement-N>
<hemanth> is how one child can have many parents
<hemanth> any o/ ?
<SourRust_> none yet
<hemanth> isinstance(class_name, obj_name )
<hemanth> is the a method which will return True or Flase
<hemanth> based on if the object is an instance of that class
<hemanth> polymorphism is not much done in py
<hemanth> but none the less can be implemented
<hemanth> like
<hemanth> class wolf(object):
<hemanth>     def bark(self):
<hemanth>         print "hooooowll"
<hemanth> class dog(object):
<hemanth>     def bark(self):
<hemanth>         print "woof"
<hemanth> def barkforme(dogtype):
<hemanth>     dogtype.bark()
<hemanth> my_dog = dog()
<hemanth> my_wolf = wolf()
<hemanth> barkforme(my_dog)
<hemanth> barkforme(my_wolf)
<hemanth> as you can see, bark()
<hemanth> method behaves differently for my_wolf and my_dog
<hemanth> there are Class Attributes and the  Instance Attributes
<hemanth> as the name says it all, Class Attributes can be accessed by ClassName.attr_name
<hemanth> >>> class x:
<hemanth> ...     a=1
<hemanth> ...
<hemanth> >>> x.a
<hemanth> 1
<hemanth> example of  Class Attribute
<hemanth> __init__ Method is one of the most useful methods
<hemanth> this method is invoked whenever an object is instantiated
<hemanth> can be called as the constructor method
<hemanth> def __init__(self, parameters):
<hemanth> is the signature of this method
<hemanth> which is useful to set default values to the attributes of the variable
<hemanth> on the same line __del__ Method if defined is called when the obj is deleted
<hemanth> garbage collector decided when to del the object
<hemanth> that is when there are no references to the obj
<hemanth> its removed from the memory
<hemanth> encapsulation of method and variables
<hemanth> can be done
<hemanth> using __method/var_name
<hemanth> as a whole the class encapsulates the methods and the variables
<hemanth> but __ emulates the private data
<hemanth> its extra protection whenever required
<hemanth> any doubts so far?
<SourRust_> uh are private variable just __<var name>?
<hemanth> yes
<SourRust_> ok
<hemanth> Python magically changes the name so that references to this attribute made in the usual way will fail
<hemanth> __getattr__(self, name):
<hemanth> will help use to know if a method is defined in the class or not
<hemanth> method/attr
<hemanth> it will  raise an AttributeError exception
<hemanth> on the same lines __setattr__ is called whenever an attribute assignment is attempted
<hemanth> clear so far?
<SourRust_> yup
* hemanth can understand that there will be many confusion, once one starts coding
<hemanth> So, thus so far is an overview of PY OOP
<hemanth> its just the raw skeleton we have seen
<hemanth> so fill in the flesh
<hemanth> make a class called CheckIT
<hemanth> and have attr as doto, done, contactNumber, so on...
<hemanth> as we talked at the beginning
<hemanth> have an __init__ method
<hemanth> have other getter and setter methods
<hemanth> have basics method to update and delete values
<hemanth> try more methods based on your abstraction
<hemanth> do also do few inheritance
<hemanth> so lot of fun stuff to do with py :)
<hemanth> tomo will be the final class!
<hemanth> pl and rb OOP remains
<SourRust_> k
<hemanth> after which we will always be in touch for more better projects
<hemanth> so if all is fine so far
<hemanth> i will make a move
<hemanth> and really sorry about n/w and other issues in b/w the class today
<SourRust_> its was annoy but we got through class :)
<hemanth> :)
<SourRust_> annoying*
<SourRust_> yes and i guess cya tomorrow