Python #8 - Classes
597
2
Advertisement
And here we finally are. Classes!
Basics
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:
Why do I have to pass an instance to the function? Why can't I do this:
So basically every class function requires the "self" parameter?
Inheritance
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:
Basics
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.x0>>> bar.f()0>>> foo.x0>>> bar.x = 5>>> bar.f()5>>> foo.f(bar)5>>> foo.x0>>> bar.test = 1>>> bar.test1Why 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 traceTypeError: 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
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): ...Advertisement
Advertisement
Advertisement
Discussion