PDA

View Full Version : [SOLVED] [Python] sequences and lists



crazyfuturamanoob
October 27th, 2008, 02:58 PM
I am really not a professional with python. When creating a class, there can be passed a parent argument, like this:

class MyClass(object):
If I want to create an object type which I can handle like a list, how do I do it?
I would like it to be like a normal list/sequence but while having some functions and stuff.
Like this:

>>>a = MyClass([1, 2, 3])
>>>print a
[1, 2, 3]
>>>a.pop(0)
>>>print a
[2, 3]
>>>a.append(16)
>>>for x in a: print x
2
3
16
>>>a.extrafunc()
I know this is possible but can't find anything with google.

y-lee
October 27th, 2008, 03:30 PM
Several ways to do that, but one of the simplest is:


class MyClass(list):

def extrafunc(self):
# whatever
return sum(self)

geirha
October 27th, 2008, 03:31 PM
class MyClass(object): will make the class inherit from the object class, it does not set the arguments for the constructor (__init__). Try inheriting from the list class.

An simple example:


class MyClass(list):
def __init__(self, sequence):
list.__init__(self, sequence)
def extrafunc(self):
return self+self

crazyfuturamanoob
October 27th, 2008, 03:35 PM
What if I want to create the list's content in it's __init__ function?
Will it be like "self = value"?

geirha
October 27th, 2008, 03:46 PM
Almost.



>>> class MyClass(list):
... def __init__(self, sequence):
... list.__init__(self, sequence)
... self.foo= 5
... def extrafunc(self):
... return self+self
...
>>> a=MyClass([1,2,3])
>>> print a
[1, 2, 3]
>>> print a.foo
5

crazyfuturamanoob
October 27th, 2008, 03:49 PM
Solved. BTW that was my 50th thanks given :D

pmasiar
October 27th, 2008, 04:19 PM
When creating a class, there can be passed a parent argument, like this:

class MyClass(object):

Wrong. When defining class, you pass it parent **class** (to inherit from) not object.

geirha
October 27th, 2008, 04:27 PM
Wrong. When defining class, you pass it parent **class** (to inherit from) not object.

Yes, and object is a class ...

pmasiar
October 27th, 2008, 07:04 PM
Yes, and object is a class ...

Not at all. Object is **instance** of a class, not the class. Don't confuse children ;-)

Class is cookie cutter, instance (object) is a cookie. You won't eat the cutter, would you?

When defining class, you provide parent class(es) to inherit from, not the instances. You cannot inherit code from the instance, it has no own code (only data): code is in class. This difference is so fundamental. I lost any confidence in your advice, geirha. Really.

I guess this is result from learning Java or C++ without understanding C and ASM: OOP becomes "magic sauce" without any reference to reality, how it is implemented. OO was just a "design pattern" in C before it became part of syntax of OOP languages.

crazyfuturamanoob
October 27th, 2008, 07:29 PM
But if I give "object" as a parent for a class, it doesn't make errors. So "object" must be another class.

gomputor
October 27th, 2008, 08:57 PM
Yes, and object is a class ...
If you are referring to
class MyClass(object): you are right (anyway you are right).


But if I give "object" as a parent for a class, it doesn't make errors. So "object" must be another class.
Yes, "object" is a class. You can say that "object" is a superclass or the baseclass. Everything is an object
and every object is an instance of a class. All classes inherit from "object" and all objects are instances
of "object".
Note that this is the new-style classes (after Python 2.2.) After Python 3.0 they will be the only
classes to exist, old-style classes will be gone. So you can stick to the new-style classes.

nvteighen
October 27th, 2008, 09:04 PM
Class is cookie cutter, instance (object) is a cookie. You won't eat the cutter, would you?

Fabulous example!!


I guess this is result from learning Java or C++ without understanding C and ASM: OOP becomes "magic sauce" without any reference to reality, how it is implemented. OO was just a "design pattern" in C before it became part of syntax of OOP languages.

Very possible, indeed. Understanding that OO is (still) done in C by creating a data structure and then defining relevant functions that accept some instance of that data structure as an argument quickly destroys that sort of "magical thinking" around OOP languages.

OOP languages implement some ways to have OOP easier by supporting useful techniques like inheritance just at hand. Inheritance in C is not trivial, but possible... if you don't believe me, please learn how GTK+ works in C.

pp.
October 27th, 2008, 09:21 PM
there can be passed a parent argument, like this:

class MyClass(object):


Wrong. When defining class, you pass it parent **class** (to inherit from) not object.

crazy&c is right because the wording of the post ('like this') implies that the letter sequence 'object' is to be taken as actual parameter. 'object' is indeed the identifier of a class and therefore is a legal value for the argument of the class declaration.

pmasiar would be right if the letter sequence 'object' was meant to be a formal parameter and and the name 'object' was used to indicate that an instance of class object was to be used as actual parameter.