Looking for real world example of Python method

0 Benjamin Grant · March 27, 2015
Like the subject says, I've seen the fake examples of methods in classes:

class Cat:
    def meow(self):
        print 'meow'

cat = Cat()
cat.meow()

And that's all well and good - but I am having trouble imagining a REAL world example where one would use a method in a class. I can imagine many examples of useful class/object PROPERTIES, but not so much methods.

The only method I can imagine is having a student class with a date of birth property and a method that returns that student's computed age - but even then, although it is a method, it's basically just messing with the properties of the object.

Are there any good example of real life methods that do more than just operate on the properties of an object?

Am I making sense?

Post a Reply

Replies

Oldest  Newest  Rating
0 Doug Fresh · March 27, 2015
Most of Python's modules are handled as Classes. Database builders are typically done as classes, and GUI's are typically built using Classes. Python has the capability of Object-Oriented programming, but it is rarely taken advantage of, due in part to everyone deciding not to use 3.XX versions of Python (my opinion). Generators are great uses of Classes that take advantage of Methods. Server maintenance and Database Maintenance are real world examples of Python actually doing something productive. If you are into Pen Testing, again, Classes will be used a lot. 

This is more of an advanced Python technique which is why you rarely see people using it or creating easily readable programs.

http://www.dabeaz.com/generators-uk/index.html

Here is a decent resource with some examples. Hopefully more people step up to provide examples!
0 Benjamin Grant · March 27, 2015
I went to that link, but the five random ones I tried did not even have classes. I was actually hoping someone could paste in this very thread a specific example of what I am looking for.

Thanks.
0 Dol Lod · March 27, 2015
Pretend you are building a game and you every actor in the game to do something like move,shoot, produce noise,go off screen,... 

Then you could generically use a method for the built in class to generically make each actor do something.

The main point of having methods defined for classes is for convenience,clarity, and polymorphism. Each class can have a totally different implementation of each method instead of having methods defined externally. Clarity is because only objects that are valid can actually use that method. Polymorphism is for having each subcalss be able to call a generic method like getArea and have everything work in a comparable way. 
  • 1

Python

107,232 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator