Class Basics

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

Class Basics

Postby ichabod801 » Fri Feb 22, 2013 3:45 pm

Content has been moved to new site at
http://python-forum.io/Thread-Class-Basics
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Class Basics

Postby ichabod801 » Mon Mar 04, 2013 10:37 pm

Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Class Basics

Postby micseydel » Tue Apr 02, 2013 8:00 am

I'd like to see more discussion about OOP rather than just describing how to use classes, and I think it would be helpful to make the magic less magical by including that
Code: Select all
instance.method(arg)

is equivalent to
Code: Select all
Class.method(instance, arg)

When I first saw that (and it wasn't in the original tutorial I found), I was like, "WHOA, that's what self means!"
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Class Basics

Postby eightbits » Sun May 19, 2013 3:11 am

Code: Select all
  class Employee(object):


In the start of the tutorial I see the class defined as the above code.
I am using python 2.7.3 on Ubuntu 12.04
I have been starting to learn python and the examples I see usually have something like:
Code: Select all
class name:

Please forgive such an elementary topic but it seems confusing.
Thanks for any advice.

Note: I must not be using the code button, the code is not displayed as I think it should(?).
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: Class Basics

Postby stranac » Tue May 21, 2013 2:54 pm

The name in the parens is the parent class.

A blank class statement creates an old-style class, while subclassing object creates a new-style class.
New style classes provide a few advantages(and have no disadvantages I know of), so they're what you should always be using.
That's why you should always inherit from object(unless you're subclassing another new-style class).

All of the above is true for python 2.
In python 3, all classes are new-style by default, so subclassing object is not necessary.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests