Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Python #8 - Classes

Sign in to follow this  


And here we finally are. Classes!

I've read a couple different tutorials about classes in Python, as well as playing around with them myself. I must say, it's quite different than C++.

First, the syntax. It's similar to everything else in python, simply "def className: definitions."

I'm still trying to figure out the difference between a class definition and an instance. Of course I know the main difference, but in terms of attributes and functions it's quite confusing. I'm used to C++ where if an attribute or function is declared static, it's a class attribute or function; otherwise it's an instance attribute or function.

First we'll just deal with attributes. In Python, it seems each class automatically has a "static" instance of each attribute already there. But if you create an instance of the class, it creates whole new instance variables. Not only that, you can dynamically create new instance variables for each object because variables don't have to be declared. Ok, that's kind of weird, but I can understand it.

Second, functions. If each attribute has a static allocation, why don't functions? Functions seem to require an instance. Is there a way to create a static function? I can't figure it out for the life of me.

Anyway, some examples should some this up:

>>> class foo:
x, y = 0, 0
def f(self):
return self.x
>>> bar = foo()
>>> bar.x
>>> bar.f()
>>> foo.x
>>> bar.x = 5
>>> bar.f()
>>> foo.f(bar)
>>> foo.x
>>> bar.test = 1
>>> bar.test

Why do I have to pass an instance to the function? Why can't I do this:

>>> class foo:
def f():
return 1
>>> foo.f()
blah blah stack trace
TypeError: unbound method f() must be called with foo instance as first argument (got nothing instead)

So basically every class function requires the "self" parameter?


Inheritance isn't really anything special in Python. One bad thing about Python classes is that there's no try data hiding. Evidentally if you put two underscores before a variable or function name the interpreter mangles the name so that you can't easily get to it, but anyone with a little determination could figure it out.

With that said, inheritance is basically just a way of reducing redundant data and logically bundling it up into common classes.

Internally, python simply creates a Dictionary and records each class attribute or function as an entry. That's what a class comes down to. Inheritance is as simple as creating a hiearchy of dictionaries, and if a definition isn't found in one, recursively search the parents'. Multiple inheritance is possible in Python, though this is usually bad design.

Anyway, the syntax is pretty easy. When defining the class, simply put the inherited classes in parenthesis:

>>> class foo(bar1, bar2):

Sign in to follow this  


Recommended Comments

I think it's a lot prettier than C and similar languages. What exactly is so ugly about it? Have you confused that which is ugly with that which is different from what you've grown used to?

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!