#๐Ÿ”’ Problem updating class attribute

21 messages ยท Page 1 of 1 (latest)

primal oasisBOT
#

@turbid hollow

Python help channel opened

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.

scarlet wing
#

I'd remove the warm_cold argument from the constructor

harsh thistle
#

!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)

primal oasisBOT
harsh thistle
#

also take a look at dataclasses if you do not have to implement things yourself

scarlet wing
#

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}")
primal oasisBOT
scarlet wing
#

curse etrotta for getting there before me

#

and yes, I chose "snek" entirely independently. Great minds &c &c

turbid hollow
#

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

harsh thistle
#

= 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

primal oasisBOT
#
Format-strings

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.

harsh thistle
#

!d str.format

primal oasisBOT
#

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.
turbid hollow
#

ok gotcha tysm!

primal oasisBOT
#
Python help channel closed

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