instance-variables

confusion because i can't figure out what is changing the attribute of the object of a class in python

confusion because i can't figure out what is changing the attribute of the object of a class in python Question: Here is my code ` class Cats: def __init__(self,name): self.name=name #print(self.name,name,"hello") def change_name(self,new_name): self.name=new_name return 0 #print(new_name) cat1=Cats("lion") print(cat1) print(cat1.name) cat2=cat1.change_name("tiger") print(cat1.name) print(cat1) print(cat2) ** Here is the output with my comments/opinion on the side …

Total answers: 1

Python dataclasses.dataclass reference to variable instead of instance variable

Python dataclasses.dataclass reference to variable instead of instance variable Question: The default values in constructors for c1 and c2 should produce new instance variables for b and b. Instead, it looks like c1.a and c2.a are referencing the same variable. Is @dataclass creating a class variable? That does not seem to be consistent with the …

Total answers: 2

Setting default/empty attributes for user classes in __init__

Setting default/empty attributes for user classes in __init__ Question: When I am creating a new class, should I set all instance attributes in __init__, even if they are None and in fact later assigned values in class methods? See example below for the attribute results of MyClass: class MyClass: def __init__(self,df): self.df = df self.results …

Total answers: 4

Python – local variable is assigned but never used – var=None

Python – local variable is assigned but never used – var=None Question: In an attempt to format my code a little better to avoid redundancies in multiple methods doing the same stuff for different classes, I’m faced with the following problem : # problematic method def a(self, b, c): result = test(b) if (result): c …

Total answers: 2

Type Hints Convention for Instance Variables Python

Type Hints Convention for Instance Variables Python Question: I’m unsure of the Python convention for type hinting instance variables – I’ve been doing them within the __init__ constructor arguments like seen here: class LoggedVar(Generic[T]): def __init__(self, value: T, name: str, logger: Logger) -> None: self.name = name self.logger = logger self.value = value` But I …

Total answers: 4

Instance variables in methods outside the constructor (Python) — why and how?

Instance variables in methods outside the constructor (Python) — why and how? Question: My questions concern instance variables that are initialized in methods outside the class constructor. This is for Python. I’ll first state what I understand: Classes may define a constructor, and it may also define other methods. Instance variables are generally defined/initialized within …

Total answers: 7

How to deal with Pylint's "too-many-instance-attributes" message?

How to deal with Pylint's "too-many-instance-attributes" message? Question: I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that the linter should not have the last …

Total answers: 4

Should all member variables be initialized in __init__

Should all member variables be initialized in __init__ Question: Maybe this is more of a style question than a technical one but I have a class with several member variables and I want to have it work so that some of the member variables are initialized when the user first creates an instance of the …

Total answers: 3