Python Forum
Help with string formatting in classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with string formatting in classes
#1
Hello all. I'm currently taking an Intro to Python course, and I'm on the lesson where we're dealing with Class composition. In the exercise, I have two Py scripts:

creating_classes.py:

class Car:
    """
    Car models a car w/ tires and an engine
    """

    def __init__(self, engine, tires):
        self.engine = engine
        self.tires = tires

    def description(self):
        print(f"A car with an {self.engine} engine, and {self.tires} tires")
tire.py:

class Tire:
    """
    Tire represents a tire that would be used with an automobile.
    """

    def __init__(self, tire_type, width, ratio, diameter,
                 brand='', construction='R'):
        self.tire_type = tire_type
        self.width = width
        self.ratio = ratio
        self.diameter = diameter
        self.brand = brand
        self.construction = construction

    def __repr__(self):
        """
        Represent the tire's information in the standard notation present
        on the side of the tire. Example: 'P215/65R15'
        """
        return (f"{self.tire_type}{self.width}/{self.ratio}"
                + f"{self.construction}{self.diameter}")
The video in the code (where the instructor is using Python 3.7) shows the output as follows:

Output:
$ python3.7 -i creating_classes.py >>> from tire import Tire >>> tire = Tire('P', 205, 55, 15) >>> tires = [tire, tire, tire, tire] >>> honda = Car(tires=tires, engine='4-cylinder') >>> honda.description() A car with a 4-cylinder engine, and [P205/55R15, P205/55R15, P205/55R15, P205/55R15] tires
However, when I run the script (using both Python 3.9 and 3.10), I get the following:
Output:
(learning_python) learning_python $ python3.10 -i creating_classes.py >>> from tire import Tire >>> tire = ('P', 205, 55, 15) >>> tires = [tire, tire, tire, tire] >>> honda = Car(tires=tires, engine='4-cylinder') >>> honda.description() A car with a 4-cylinder engine and [('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15)] tires
Any idea why the output is formatted this way, as opposed to the way it should be, with proper formatting in the fstring?

Thanks for looking!
Reply
#2
Is there a particular output you expect? Other than what you get? This is in fact pretty correct what is printed. Because it is printing the whole object (meaning it is printing al the items from the list from the tires). If you want particular items from the tire object, a different approach is needed.
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#3
I expected this:

Output:
A car with a 4-cylinder engine, and [P205/55R15, P205/55R15, P205/55R15, P205/55R15] tires
I got this:
Output:
A car with a 4-cylinder engine and [('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15)] tires
The script ignored the formatting of the fstring in tire.py. When I run it in 3.7, it works as expected. Obviously something changed between 3.7 and 3.9, I'm just wondering if it is a bug or if there's now another way to code in 3.9+ to get the desired formatting.
Reply
#4
The difference is, You wrote:
tire = ('P', 205, 55, 15)
Where as the instructor wrote:
tire = Tire('P', 205, 55, 15)
Jeff900 likes this post
Reply
#5
(Dec-17-2021, 04:27 PM)BashBedlam Wrote: The difference is, You wrote:
tire = ('P', 205, 55, 15)
Where as the instructor wrote:
tire = Tire('P', 205, 55, 15)

You're my hero. I've been banging my head against the wall.
Reply
#6
(Dec-17-2021, 04:19 PM)brthurr Wrote: I expected this:

Output:
A car with a 4-cylinder engine, and [P205/55R15, P205/55R15, P205/55R15, P205/55R15] tires
I got this:
Output:
A car with a 4-cylinder engine and [('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15)] tires
The script ignored the formatting of the fstring in tire.py. When I run it in 3.7, it works as expected. Obviously something changed between 3.7 and 3.9, I'm just wondering if it is a bug or if there's now another way to code in 3.9+ to get the desired formatting.

I tried with 3.9.1 and the result is correct.

Output:
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from car_tire import Car, Tire >>> tire = Tire('P', 205, 55, 15) >>> tires = [tire, tire, tire, tire] >>> tires [P205/55R15, P205/55R15, P205/55R15, P205/55R15] >>> honda = Car(tires=tires, engine='4-cylinder') >>> honda.description() A car with an 4-cylinder engine, and [P205/55R15, P205/55R15, P205/55R15, P205/55R15] tires
The only difference is that I putted both classes in de same file and imported them both with the import statement.
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#7
(Dec-17-2021, 04:27 PM)BashBedlam Wrote: The difference is, You wrote:
tire = ('P', 205, 55, 15)
Where as the instructor wrote:
tire = Tire('P', 205, 55, 15)

Sharp! I didn't saw it either. But that explains a lot.
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting a date time string read from a csv file DosAtPython 5 1,306 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 854 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 2,011 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 2,078 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Question on HTML formatting with set string in message Cknutson575 3 3,514 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,666 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  String formatting difficulties mmk1995 3 2,797 Aug-09-2019, 11:18 AM
Last Post: wavic
  string formatting Uchikago 1 1,934 Jun-28-2019, 03:28 PM
Last Post: buran
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 14,983 Jul-15-2018, 08:51 PM
Last Post: ichabod801
  formatting string and returning as geojson garynobles 12 6,662 Mar-06-2018, 05:02 PM
Last Post: garynobles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020