Some problems with classes

S

ssecorp

Why/how is it possible to add variables like this? I don't understand
this mechanism:
http://docs.python.org/tut/node11.html#SECTION0011330000000000000000

class Employee:
pass

john = Employee() # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

---------------------------------------------------------------------------

Also, I can't get multiple inheritance to work.

Don't mind that the a vegan obviously don't inherit from an animal and
a vegetable. I didn't come up with anything better, it is just to
learn about inheritance.


class Animal(object):
def __init__(self, name, weight):
self.name = name
self.weight = weight

def speak(self):
print "speak"

class Vegetable(object):
def __init__(self, name, volume):
self.name = name
self.volume = volume

def split(self):
print "tjoff"

class Vegan(Animal, Vegetable):
#pass
def __init__(self, name, attacks):
self.name = name
self.attacks = attacks

Traceback (most recent call last):
File "C:/Python25/Progs/XXXX/Movie.py", line 42, in <module>
class ActionComedy(Movie, ActionMovie):
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases Movie, ActionMovie

also, when inheriting, can I inherit __init__() somehow? If I want the
same attributes but perhaps some additional methods for example.
 
C

Chris Rebert

Why/how is it possible to add variables like this? I don't understand
this mechanism:
http://docs.python.org/tut/node11.html#SECTION0011330000000000000000

Under the covers, Python objects are implemented using dictionaries,
so adding an attribute just adds a new key-value pair to the object's
internal dictionary (which, incidentally, you can access as
someobj.__dict__).
class Employee:
pass

john = Employee() # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

---------------------------------------------------------------------------

Also, I can't get multiple inheritance to work.

Don't mind that the a vegan obviously don't inherit from an animal and
a vegetable. I didn't come up with anything better, it is just to
learn about inheritance.


class Animal(object):
def __init__(self, name, weight):
self.name = name
self.weight = weight

def speak(self):
print "speak"

class Vegetable(object):
def __init__(self, name, volume):
self.name = name
self.volume = volume

def split(self):
print "tjoff"

class Vegan(Animal, Vegetable):
#pass
def __init__(self, name, attacks):
self.name = name
self.attacks = attacks


Traceback (most recent call last):
File "C:/Python25/Progs/XXXX/Movie.py", line 42, in <module>
class ActionComedy(Movie, ActionMovie):
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases Movie, ActionMovie

The class names in error message here don't match the classes you
declared above. Give us the actual code you're using or at least a
stub version.
also, when inheriting, can I inherit __init__() somehow? If I want the
same attributes but perhaps some additional methods for example.

Use super() to call your superclasses' __init__() methods. See
super()'s entry in http://docs.python.org/lib/built-in-funcs.html for
more info.

- Chris
 
S

ssecorp

class Animal(object):
def __init__(self, name, weight):
self.name = name
self.weight = weight

def speak(self):
print "speak"

class Vegetable(object):
def __init__(self, name, volume):
self.name = name
self.volume = volume

def split(self):
print "tjoff"

class Vegan(Animal, Vegetable):
def __init__(self, name, attacks):
self.name = name
self.attacks = attacks
 
S

ssecorp

also, how does super() work more exactly? I can't get it quite to
work.


class Movie(object):
def __init__(self, movieId, grades, date):
self.movieId = movieId
self.grades = grades
self.date = date

def newGrade(self, grade):
self.grades.append(grade)

def spam(self):
print "inherits all the way down?"

def averageGrade(self):
return sum(grade for grade in self.grades) / \
len(self.grades)

class ActionMovie(Movie):
super(Movie)
##def __init__(self, movieId, grades, date, kills):
## self.movieId = movieId
## self.grades = grades
## self.date = date
## self.kills = kills

def newGrade(self, grade, date):
self.grades.append(grade)
self.date = date

def prd(self):
print self.date

class Comedy(ActionMovie):
def __init__(self, movieId, grades, date):
self.movieId = movieId
self.grades = grades
self.date = date

def donk(self):
print "im a donkey!"


subclasses has to be indented?
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
 
S

ssecorp

It works when I inherit from 2 classes but not when I inherit from 2
subclasses.


-----------------------------------------------------

from __future__ import division

class Movie(object):
def __init__(self, movieId, grades, date):
self.movieId = movieId
self.grades = grades
self.date = date

def newGrade(self, grade):
self.grades.append(grade)

def spam(self):
print "inherits all the way down?"

def averageGrade(self):
return sum(grade for grade in self.grades) / \
len(self.grades)

class ActionMovie(Movie):
#super(Movie, self)
def __init__(self, movieId, grades, date, kills):
self.movieId = movieId
self.grades = grades
self.date = date
self.kills = kills

def newGrade(self, grade, date):
self.grades.append(grade)
self.date = date

def prd(self):
print self.date

class Comedy(ActionMovie):
def __init__(self, movieId, grades, date):
self.movieId = movieId
self.grades = grades
self.date = date

def donk(self):
print "im a donkey!"

##class ActionComedy(Movie, ActionMovie):
## def __init__(self, movieId, grades, date):
## self.movieId = movieId
## self.grades = grades
## self.date = date



class Animal(object):
def __init__(self, name, weight):
self.name = name
self.weight = weight

def speak(self):
print "speak"

class Vegetable(object):
def __init__(self, name, volume):
self.name = name
self.volume = volume

def split(self):
print "tjoff"

class Vegan(Animal, Vegetable):
#pass
#super()
def __init__(self, name, attacks):
self.name = name
self.attacks = attacks
 
M

Michele Simionato

Traceback (most recent call last):
  File "C:/Python25/Progs/XXXX/Movie.py", line 42, in <module>
    class ActionComedy(Movie, ActionMovie):
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases Movie, ActionMovie

The MRO is explained here:
http://www.python.org/download/releases/2.3/mro/

Super is explained here:
http://www.artima.com/weblogs/index.jsp?blogger=micheles

Be warned that those are not readings for a beginner, but
if you want to use multiple inheritance and super you must
be prepared ;)
 
B

Bruno Desthuilliers

Chris Rebert a écrit :
Under the covers, Python objects are implemented using dictionaries,

Not necessarily.
so adding an attribute just adds a new key-value pair to the object's
internal dictionary (which, incidentally, you can access as
someobj.__dict__).

Idem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top