#๐ Problem updating class attribute
21 messages ยท Page 1 of 1 (latest)
@turbid hollow
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.
I'd remove the warm_cold argument from the constructor
!e you can just specify it inside of Reptile's constructor, when you call Animal's init through super() ```py
class Animal:
def init(self, habitat, leg_count, movement, warm_cold, species):
self.habitat = habitat
self.leg_count = leg_count
self.movement = movement
self.warm_cold = warm_cold
self.species = species
def __str__(self):
return '\nHabitat: ' + self.habitat + '\nLeg count: ' + str(self.leg_count) + '\nMovement: ' + self.movement + '\nWarm or Cold Blooded: ' + self.warm_cold + '\nSpecies: ' + self.species
class Reptile(Animal):
def init(self, habitat, leg_count, movement, species):
super().init(habitat, leg_count, movement, "Cold Blooded", species)
snek = Reptile("pydis", 0, "wiggle", "snek")
print(snek)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 |
002 | Habitat: pydis
003 | Leg count: 0
004 | Movement: wiggle
005 | Warm or Cold Blooded: Cold Blooded
006 | Species: snek
also take a look at dataclasses if you do not have to implement things yourself
ya, here's how I wound up doing it
!e
class Animal:
def __init__(self, habitat, leg_count, movement, species, warm_cold="neither"):
self.habitat = habitat
self.leg_count = leg_count
self.movement = movement
self.warm_cold = warm_cold
self.species = species
def __str__(self):
return (
"\nHabitat: "
+ self.habitat
+ "\nLeg count: "
+ str(self.leg_count)
+ "\nMovement: "
+ self.movement
+ "\nWarm or Cold Blooded: "
+ self.warm_cold
+ "\nSpecies: "
+ self.species
)
class Reptile(Animal):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.warm_cold = "Cold Blooded"
cat = Animal(
habitat="couch",
leg_count=4,
movement="pouncing",
warm_cold="warm",
species="cattus cattus",
)
print(f"{cat=!s}")
snek = Reptile(
habitat="terrariun",
leg_count=0,
movement="slither",
species="snekulous",
)
print(f"{snek=!s}")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | cat=
002 | Habitat: couch
003 | Leg count: 4
004 | Movement: pouncing
005 | Warm or Cold Blooded: warm
006 | Species: cattus cattus
007 | snek=
008 | Habitat: terrariun
009 | Leg count: 0
010 | Movement: slither
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/3OPVS7SBF6LHP7MME4PXJ3AVEA
curse etrotta for getting there before me
and yes, I chose "snek" entirely independently. Great minds &c &c
Lol ok thanks so much guys. That works well. I am pretty new to this so I'm still learning how classes work.
offby1 what does this do in the code you did?
print(f"{snek=!s}")
specifically the =!s
= includes the name of the variable (or more precisely, the expression being formatted into the string)
!s tells it to use __str__ (as opposed to __format__ or __repl__)
!fstring
Creating a Python string with your variables using the + operator can be difficult to write and read. F-strings (format-strings) make it easy to insert values into a string. If you put an f in front of the first quote, you can then put Python expressions between curly braces in the string.
>>> snake = "pythons"
>>> number = 21
>>> f"There are {number * 2} {snake} on the plane."
"There are 42 pythons on the plane."
Note that even when you include an expression that isn't a string, like number * 2, Python will convert it to a string for you.
!d str.format
str.format(*args, **kwargs)```
Perform a string formatting operation. The string on which this method is called can contain literal text or replacement fields delimited by braces `{}`. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. Returns a copy of the string where each replacement field is replaced with the string value of the corresponding argument.
```py
>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'
``` See [Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings) for a description of the various formatting options that can be specified in format strings.
ok gotcha tysm!
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.
๐ Problem updating class attribute