Home > other >  Python classes and objects
Python classes and objects

Time:12-04

Why do wrong??

Class B:
X=1
Def delshu (self) :
Del self. X
> B=b ()
> B.x
1
> B.d elshu ()
Traceback (the most recent call last) :
The File "& lt; Pyshell# 96 & gt;" , line 1, in
B.d elshu ()
The File "& lt; Pyshell# 93 & gt;" , line 4, in delshu
Del self. X
AttributeError: x

CodePudding user response:

Class B:
X=1
Here X is the class attribute,
Del self. X
Here the self. X is the instance attribute
The key to the
Del self. X
Delete the instance attributes, this instance b has a self. This instance attributes x???

CodePudding user response:

X=1 there must write self. X=1. Write directly x represents the attributes of a class rather than the object's properties

CodePudding user response:

Instantiated attribute instance objects don't have class attribute?
B=b ()
B.x can call
Is that the b.x class attribute, or instance attributes
  • Related