#๐ basic OOP
81 messages ยท Page 1 of 1 (latest)
@alpine wagon
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Hey @alpine wagon!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
class Vehicle:
vehicle_id = 0
def __init__(self, mileage=None, tank_capacity=None, remaining_fuel=None, fuel_efficiency=None ) -> None:
'''
Purpose: declaring vehicle objects values that will be used by the methods below
Parameters: int: mileage, tank_capacity. float: remaining_fuel, fuel_efficiency.
Return:None
'''
#conditionals that create default values for the object if none are given
self.mileage = 0 if mileage is None else mileage
self.tank_capacity = 0 if tank_capacity is None else tank_capacity
self.remaining_fuel = 0.0 if remaining_fuel is None else remaining_fuel
self.fuel_efficiency = 0.0 if fuel_efficiency is None else fuel_efficiency
#conter for the object ID
Vehicle.vehicle_id += 1
๐
Did you have a question about this?
yea sorry it deleted my first post
let me see if I can recover it
we are making Vehicle objects that require an id number that can simply be an integer that increases. ie: (v1 has an id of 1) (v2 has an id of 2) and so on. code is as follows
thank you
im running into a problem where if i create v1 first as an empty object then try and reinitialize it the id will be incremented again
v1 = Vehicle (100 ,50 ,0 ,10)
you could decrease the id inside __del__
but also unless you absolutely need the ids to perfectly incremental, it's totally fine like this
this guarantees each vehicle id will be unique since they'll never be re-used
im pretty sure its an oversight from my lab instructor as they initialize it this way and show the v1 as having id 1 even after doing what ive done above. is there anyway to recognize that they are meant to be the same?
I really think it should be ok
variable names shouldn't ever really be important
the fact that v1 has an id of 1 shouldn't really matter
if it was more important, it should be a parameter of init
okay i do have one more question that may change things
sure, go ahead
in their example they call "print ( repr ( v3 ))"
and they get
Type : < class ' main . Vehicle '> id :2490535350096
this makes it seem like they are using something else for an id
are you meant to be defining your own __repr__
otherwise that doesn't quite look like a default output
yes i was following on of their examples in class but it is a long way off of this
are you sure they aren't also printing id(v3)?
def __repr__(self) -> str:
'''
Purpose: returns a string for debugging
Parameters: None
Return: details for debugging in (str) format
'''
return f"Type: {type(Vehicle)} Vehicle({self.mileage}, {self.tank_capacity}, {self.remaining_fuel}, {self.fuel_efficiency})"
also i cannot seem to get the py syntax to work
success
nice!
f"Type: {type(Vehicle)}
this isn't quite right here
I wouldn't say Type: here either, it should just be Vehicle there
if you want a raw string of a class name, you can use self.__class__.__name__
it's a bit lengthy though, haha
!E
data = 'hello'
print(data.__class__.__name__)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
str
!e
data = 'hello'
print(type(data))
print(data.__class__.__name__)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <class 'str'>
002 | str
as opposed to type() that has some other text with it
is the id() function unique in that i could assign a single digit to it?
the id of every instance will be unique, yes
I wouldn't expect a single digit though
what if you had 100 vehicles?
it is not very nice no so i suppose i could just keep my current id system and then print id() to match the dunder that they have
it's not entirely clear from your example what id they expect you to use
I think the incrementing id you have using the class variable is great
but you should also store it as an instance attribute
for example
print ( v3 )
Vehicle 's id : 3 , mileage :1000 KM , fuel efficiency :8 KM / L
self.id = Vehicle.vehicle_id
i am going to show you the whole lab output block in a second
>>> v1 = Vehicle ()
>>> v1 = Vehicle (100 ,50 ,0 ,10)
>>> print ( v1 )
Vehicle 's id : 1 , mileage :100 KM , fuel efficiency :10 KM / L
>>> v1 . move (100)
>>> print ( v1 )
Vehicle 's id : 1 , mileage :100 KM , fuel efficiency :10 KM / L
>>> v1 . refuel (100)
>>> print ( v1 . get_remaining_fuel ())
50
>>> v1 . move (50)
>>> print ( v1 . get_remaining_fuel ())
45.0
>>> print ( v1 . get_mileage ())
150
>>> print ( v1 )
Vehicle 's id : 1 , mileage :150 KM , fuel efficiency :10 KM / L
>>> v2 = Vehicle (10000 ,50 ,20 ,20)
>>> print ( v2 )
Vehicle 's id : 2 , mileage :10000 KM , fuel efficiency :20 KM / L
>>> v2 . move (100)
>>> print ( v2 . get_remaining_fuel ())
15.0
>>> print ( v2 . get_mileage ())
10100
>>> print ( v2 )
Vehicle 's id : 2 , mileage :10100 KM , fuel efficiency :20 KM / L
>>> print ( v1 > v2 )
True
>>> v3 = Vehicle (1000 ,60 ,60 ,8)
>>> v3 . refuel ( -50)
>>> print ( v3 )
Vehicle 's id : 3 , mileage :1000 KM , fuel efficiency :8 KM / L
>>> print ( repr ( v3 ))
Type : < class ' __main__ . Vehicle '> id :2490535350096
4
>>> v3 . move (1000)
>>> print ( v3 . get_mileage ())
1480
>>> print ( v3 . get_remaining_fuel ())
0
>>> print ( v2 < v3 < v1 )
True
>>> v3 . set_fuel_efficiency (20)
>>> v1 . set_fuel_efficiency (1)
>>> print ( v3 < v1 )
False
it feels like some of the lab is solving a puzzle because we are given this and expected to create its output
this is definitely odd, but it seems like the id here is NOT the same expected as the vehicle's id
this is the memory id
!e
class Vehicle:
def __repr__(self):
return f'Type: <class {__name__}.{self.__class__.__name__}> id: {id(self)}'
v1 = Vehicle()
print(v1)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Type: <class __main__.Vehicle> id: 139791998827024
so when i use id() its different everytime becaue im never actually storing it right? so it shouldnt be the same unless it is saved or im keeping to instance alive
correct
you'll never need to manually manipulate the id()
it's the memory address, you have no control over it
as for this, it MIGHT just be showing you that you should be able to create an instance without any arguments
i am able to do that yes
so the fact that the id for the vehicle is 1 might be a mistake
but if you did want it to go back to 1, you can use my original suggestion of implementing __del__
and i think it is a mistake as he was confused when we asked him why it was there but he never fixed it
but that could lead to duplicate vehicle id
so it's a bad idea
yeah, I wouldn't worry about that
just increase the count for each vehicle instance and that's good enough
that is all i need then. thanks for all your help
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ basic OOP