Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 10:42, Vusa Moyo wrote:
> Thanks so much. You've been a great help. 
>
> You have confirmed that the lecture's question is flawed.

It is not, it is exactly right.
(Albeit unusual in its use of class attributes) but there is
nothing wrong with the code, only the way you were trying to use it.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Vusa Moyo
Thanks so much. You've been a great help.

You have confirmed that the lecture's question is flawed.

Appreciate the help.

Regards

Vusa

On Thu, Feb 9, 2017 at 12:02 PM, Alan Gauld via Tutor 
wrote:

> On 09/02/17 09:25, Vusa Moyo wrote:
>
> > class Cat:
> > name = ""
> > kind = "cat"
> > color = ""
> > value = 100.00
> >
> > def description(self):
> > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
> > self.color, self.kind, self.value)
> > return desc_str
> >
> > The above code is the question, which I am not allowed to edit.
> >
> > So just to test the lecturer's code, I run the command
> >
> > print(Cat.description())
>
> But the definition of description() take an argument - self.
> self is expected to be an instance of Cat.
> You can either pass that in manually
>
> print( Cat.description(Cat()) )
>
> or, more normally, create an instance of cat and call
> description on that:
>
> my_cat = Cat()
> print( my_cat.description() )
>
> > Any other code I append to it by inheriting the class Cat, will still
> have
> > that similar error.
>
> I'm not sure what you mean by that, I'd need an example.
> If you mean you just add the code after the above line then
> obviously you will still get the error.
>
> > Now, I've added the following code to inherit the class Cat: description.
> >
> > class Cat1(Cat):
> > name = "Whiskers"
> > kind = "Burmese cat"
> > color = "grey"
> > value = 3000.00
> >
> > When I run this command, I still receive the same error.
> >
> > print(Cat1.description())
>
> For the same reason; you are still not passing an instance
> of Cat (or Cat1) to the method. You need to create an
> instance and then call the method on that:
>
> other_cat = Cat1()
> print( other_cat.description() )
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Alan Gauld via Tutor
On 09/02/17 09:25, Vusa Moyo wrote:

> class Cat:
> name = ""
> kind = "cat"
> color = ""
> value = 100.00
> 
> def description(self):
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
> self.color, self.kind, self.value)
> return desc_str
> 
> The above code is the question, which I am not allowed to edit.
> 
> So just to test the lecturer's code, I run the command
> 
> print(Cat.description())

But the definition of description() take an argument - self.
self is expected to be an instance of Cat.
You can either pass that in manually

print( Cat.description(Cat()) )

or, more normally, create an instance of cat and call
description on that:

my_cat = Cat()
print( my_cat.description() )

> Any other code I append to it by inheriting the class Cat, will still  have
> that similar error.

I'm not sure what you mean by that, I'd need an example.
If you mean you just add the code after the above line then
obviously you will still get the error.

> Now, I've added the following code to inherit the class Cat: description.
> 
> class Cat1(Cat):
> name = "Whiskers"
> kind = "Burmese cat"
> color = "grey"
> value = 3000.00
> 
> When I run this command, I still receive the same error.
> 
> print(Cat1.description())

For the same reason; you are still not passing an instance
of Cat (or Cat1) to the method. You need to create an
instance and then call the method on that:

other_cat = Cat1()
print( other_cat.description() )


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-09 Thread Vusa Moyo
Hi Alan.

You are correct with the indentation.

class Cat:
name = ""
kind = "cat"
color = ""
value = 100.00

def description(self):
desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
self.color, self.kind, self.value)
return desc_str

The above code is the question, which I am not allowed to edit.

So just to test the lecturer's code, I run the command

print(Cat.description())

This returns an error.

 TypeError: description() missing 1 required positional argument: 'self'

To me, this is flawed. I should be able to get a fault less response from
that command.

Any other code I append to it by inheriting the class Cat, will still  have
that similar error.

Now, I've added the following code to inherit the class Cat: description.

class Cat1(Cat):
name = "Whiskers"
kind = "Burmese cat"
color = "grey"
value = 3000.00

When I run this command, I still receive the same error.

print(Cat1.description())

Please assist where possible.

Regards

Vusa

On Wed, Feb 8, 2017 at 11:06 AM, Alan Gauld via Tutor 
wrote:

> On 08/02/17 07:11, Vusa Moyo wrote:
> > I have a suspicion my lecturer's question is flawed, so I'd like to pose
> it
> > to you guys to confirm my suspicions.
>
> I think your interpretation of the question is flawed.
> See Peter's reply for why.
>
> However another point is
>
> >  class Cat:
> >  name = ""
> >  kind = "cat"
> >  color = ""
> >  value = 100.00
> >  def description(self):
> >
> > desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> > self.kind, self.value)
>
> Python is sensitive to indentation. This line needs
> to be indented inside the def statement. (This may
> be a mail formatting issue but since the rest of
> your code looks OK I doubt it)
>
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-08 Thread Alan Gauld via Tutor
On 08/02/17 07:11, Vusa Moyo wrote:
> I have a suspicion my lecturer's question is flawed, so I'd like to pose it
> to you guys to confirm my suspicions.

I think your interpretation of the question is flawed.
See Peter's reply for why.

However another point is

>  class Cat:
>  name = ""
>  kind = "cat"
>  color = ""
>  value = 100.00
>  def description(self):
> 
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> self.kind, self.value)

Python is sensitive to indentation. This line needs
to be indented inside the def statement. (This may
be a mail formatting issue but since the rest of
your code looks OK I doubt it)



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Multiple Inheritance in Classes

2017-02-08 Thread Peter Otten
Vusa Moyo wrote:

> I have a suspicion my lecturer's question is flawed, so I'd like to pose
> it to you guys to confirm my suspicions.
> 
> Here goes..
> 
> I've gone and created a Class Cat1(cat): <-- inherited class, but cant
> seem get the code right which allows the test code to run successfully.

Is the following... 

> We have a class defined for cats. Create a new cat called cat1. Set cat1
> to be a grey Burmese cat worth 3000 with the name Whiskers.

your task? Then your lecturer may be asking you to *instantiate* the Cat 
class, not to make a subclass.

>  # define the Cat class
> 
>  class Cat:
>  name = ""
> 
>  kind = "cat"
>  color = ""
>  value = 100.00

The above are all class attributes. This is unusual, as even two cates of 
the same race will have a different name and value. If I'm guessing right 
your solution will look more like

$ cat cars.py
class Car:
def __init__(self, make, color, year):
self.make = make
self.color = color
self.year = year

def __str__(self):
return "{0.color} {0.make}".format(self)

def __repr__(self):
return (
"Car(color={0.color}, "
"make={0.make}, "
"year={0.year})"
).format(self)

carpark = [
Car("Ford", "blue", 1973),
Car("Volkswagen", "yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
print("- a", car)  # uses Car.__str__
$ python3 cars.py 
[Car(color=blue, make=Ford, year=1973), Car(color=yellow, make=Volkswagen, 
year=2003)]

In our car park we have
- a blue Ford
- a yellow Volkswagen
$ 

>From the view of the script a VW and a Ford work the same, so there is no 
need to introduce subclasses, but if you're asked to do it anyway here's one 
way:

$ cat cars2.py
class Car:
def __init__(self, color, year):
self.color = color
self.year = year

def __str__(self):
return "{0.color} {0.make}".format(self)

def __repr__(self):
return (
"{classname}(color={0.color}, "
"year={0.year})"
).format(self, classname=self.__class__.__name__)

class Ford(Car):
make = "Ford"

class Volkswagen(Car):
make = "VW"

carpark = [
Ford("blue", 1973),
Volkswagen("yellow", 2003)
]

print(carpark)  # uses Car.__repr__
print()

print("In our car park we have")
for car in carpark:
print("- a", car)  # uses Car.__str__
$ python3 cars2.py 
[Ford(color=blue, year=1973), Volkswagen(color=yellow, year=2003)]

In our car park we have
- a blue Ford
- a yellow VW
$ 

However, this is the kind of inheritance you will only ever see in textbook 
examples because the subclasses do not introduce differing features. As a 
rule of thumb there should be at least one method with a fundamentally 
different implementation, or one extra method that is not shared between 
baseclass and subclass.

>  def description(self):
> desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
> self.kind, self.value)
> 
>  return desc_str
> 
> # your code goes here
> 
> 
> # test code
> 
>  print(cat1.description())



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with Multiple Inheritance in Classes

2017-02-07 Thread Vusa Moyo
I have a suspicion my lecturer's question is flawed, so I'd like to pose it
to you guys to confirm my suspicions.

Here goes..

I've gone and created a Class Cat1(cat): <-- inherited class, but cant seem
get the code right which allows the test code to run successfully.

We have a class defined for cats. Create a new cat called cat1. Set cat1 to
be a grey Burmese cat worth 3000 with the name Whiskers.

 # define the Cat class

 class Cat:
 name = ""

 kind = "cat"
 color = ""
 value = 100.00
 def description(self):

desc_str = "%s is a %s %s cat worth R%.2f." % (self.name, self.color,
self.kind, self.value)

 return desc_str

# your code goes here


# test code

 print(cat1.description())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor