Hướng dẫn can we change class variable in python? - chúng ta có thể thay đổi biến lớp trong python không?

Được rồi, lần này tôi sẽ cố gắng và cực kỳ rõ ràng.

class Yes:

    def __init__(self):
        self.a=1

    def yes(self):
        if self.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if self.a==1:
            print "No"
        else:
            print "Yes, but no"
        self.a-=1 #Note this line

Bây giờ, trong khi chạy:

Yes().yes()
No().no()
Yes().yes()
No().no()

Tôi muốn nó in ra:

Yes
No
No, but yes
Yes, but no

Nó đưa cho tôi:

Yes
No
Yes
No

Bây giờ, tôi biết lý do tại sao là vì tôi chỉ thay đổi giá trị của bản thân. Trong không có lớp (hãy nhớ dòng đó?). Tôi muốn biết liệu dù sao cũng có thể thay đổi nó trong lớp có trong khi vẫn ở trong lớp không (như nếu có thứ gì đó mà tôi có thể cắm vào vị trí của bản thân.a- = 1 sẽ hoạt động).

Hỏi ngày 3 tháng 4 năm 2013 lúc 14:42Apr 3, 2013 at 14:42

Hướng dẫn can we change class variable in python? - chúng ta có thể thay đổi biến lớp trong python không?

4

Tôi không chắc bạn có thể sử dụng gì cho việc này, nhưng ...

Bạn muốn thao tác một biến lớp, nhưng bạn tiếp tục giải quyết các biến thể hiện. Nếu bạn muốn một biến lớp, hãy sử dụng một biến lớp!

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var

Đã trả lời ngày 3 tháng 4 năm 2013 lúc 14:51Apr 3, 2013 at 14:51

Francis Avilafrancis AvilaFrancis Avila

30.8k6 Huy hiệu vàng57 Huy hiệu bạc95 Huy hiệu Đồng6 gold badges57 silver badges95 bronze badges

6

Nó xuất hiện những gì bạn muốn sử dụng là một biến tĩnh chứ không phải là một biến thể hiện. Một biến tĩnh được chia sẻ giữa tất cả các trường hợp của lớp.

class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()

Sẽ đầu ra:

Yes
No
No, but yes
Yes, but no

Đã trả lời ngày 3 tháng 4 năm 2013 lúc 14:52Apr 3, 2013 at 14:52

Jesse Vogtjesse VogtJesse Vogt

15.9K14 Huy hiệu vàng58 Huy hiệu bạc71 Huy hiệu đồng14 gold badges58 silver badges71 bronze badges

1

Trong thực tế trước đây, chúng ta đã thấy rằng Python không có từ khóa tĩnh. Tất cả các biến được gán một giá trị trong khai báo lớp là các biến lớp

Chúng ta nên cẩn thận khi thay đổi giá trị của biến lớp. Nếu chúng ta cố gắng thay đổi biến lớp bằng cách sử dụng đối tượng, một biến thể mới (hoặc không tĩnh) cho đối tượng cụ thể đó sẽ được tạo và biến này, các biến lớp này. Dưới đây là chương trình Python để chứng minh điều tương tự.. If we try to change class variable using object, a new instance (or non-static) variable for that particular object is created and this variable shadows the class variables. Below is Python program to demonstrate the same.

class

Yes().yes()
No().no()
Yes().yes()
No().no()
0

Yes().yes()
No().no()
Yes().yes()
No().no()
1
Yes().yes()
No().no()
Yes().yes()
No().no()
2
Yes().yes()
No().no()
Yes().yes()
No().no()
3 ________ 14 & nbsp; & nbsp; & nbsp; & nbsp;

Yes().yes()
No().no()
Yes().yes()
No().no()
1
Yes().yes()
No().no()
Yes().yes()
No().no()
6
Yes().yes()
No().no()
Yes().yes()
No().no()
7
Yes().yes()
No().no()
Yes().yes()
No().no()
8
Yes().yes()
No().no()
Yes().yes()
No().no()
9

Yes
No
No, but yes
Yes, but no
0
Yes().yes()
No().no()
Yes().yes()
No().no()
8
Yes
No
No, but yes
Yes, but no
22213
Yes
No
No, but yes
Yes, but no
4

Yes
No
No, but yes
Yes, but no
0
Yes().yes()
No().no()
Yes().yes()
No().no()
8
Yes
No
No, but yes
Yes, but no
7
Yes().yes()
No().no()
Yes().yes()
No().no()
3
Yes
No
No, but yes
Yes, but no
9

Yes
No
Yes
No
0
Yes().yes()
No().no()
Yes().yes()
No().no()
3
Yes
No
Yes
No
2
Yes
No
Yes
No
3
Yes
No
Yes
No
4
Yes
No
Yes
No
5
Yes
No
Yes
No
6

Yes
No
Yes
No
7
Yes().yes()
No().no()
Yes().yes()
No().no()
3
Yes
No
Yes
No
2
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
0
Yes
No
Yes
No
4
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
2
Yes
No
Yes
No
6

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
5

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
7
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
8

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
0
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
1

class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
2
Yes().yes()
No().no()
Yes().yes()
No().no()
3
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
4

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
6

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
7
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
8

class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
4
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
0
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
1

Output:

Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse

class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
2
Yes().yes()
No().no()
Yes().yes()
No().no()
3
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
4

Yes
No
No, but yes
Yes, but no
3

Yes
No
No, but yes
Yes, but no
4

Yes
No
No, but yes
Yes, but no
5

class

Yes().yes()
No().no()
Yes().yes()
No().no()
0

Yes().yes()
No().no()
Yes().yes()
No().no()
1
Yes().yes()
No().no()
Yes().yes()
No().no()
2
Yes().yes()
No().no()
Yes().yes()
No().no()
3 ________ 14 & nbsp; & nbsp; & nbsp; & nbsp;

Yes().yes()
No().no()
Yes().yes()
No().no()
1
Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
3

Yes
No
No, but yes
Yes, but no
0
Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
5

Yes
No
No, but yes
Yes, but no
0
Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
7

Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
8

Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
9
a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
0
a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
1

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
2
a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
3
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
8

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
5

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
6
a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
7

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
2
a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
9

Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse
8

class1class2class3

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
2class5

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
2
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
7
class Yes:
    a = 1 # initialize class var.
    def __init__(self):
        self.a = 1 # point of this is what?

    def yes(self):
        if Yes.a==1: # check class var
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1: # check class var
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 # alter class var
8

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec
2
class Yes:
    a = 1
    def __init__(self):
        pass

    def yes(self):
        if Yes.a==1:
            print "Yes"
        else:
            print "No, but yes"

class No(Yes):

    def no(self):
        if Yes.a==1:
            print "No"
        else:
            print "Yes, but no"
        Yes.a-=1 #Note this line

Yes().yes()
No().no()
Yes().yes()
No().no()
0
Yes
No
No, but yes
Yes, but no
2

Yes().yes()
No().no()
Yes().yes()
No().no()
1
Yes().yes()
No().no()
Yes().yes()
No().no()
6
Yes().yes()
No().no()
Yes().yes()
No().no()
7
Yes().yes()
No().no()
Yes().yes()
No().no()
8
Yes().yes()
No().no()
Yes().yes()
No().no()
9

a.tream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec

Yes
No
No, but yes
Yes, but no
0
Yes().yes()
No().no()
Yes().yes()
No().no()
8
Yes
No
No, but yes
Yes, but no
22213
Yes
No
No, but yes
Yes, but no
4

00 0

Yes No No, but yes Yes, but no 0Yes().yes() No().no() Yes().yes() No().no() 8Yes No No, but yes Yes, but no 7Yes().yes() No().no() Yes().yes() No().no() 3 Yes No No, but yes Yes, but no 9

Yes No Yes No 0Yes().yes() No().no() Yes().yes() No().no() 3 Yes No Yes No 2Yes No Yes No 3Yes No Yes No 4Yes No Yes No 5Yes No Yes No 6

Có thể thay đổi một biến lớp không?

Bất kỳ đối tượng nào cũng có thể thay đổi giá trị của biến lớp, nhưng các biến lớp cũng có thể được thao tác mà không tạo ra một thể hiện của lớp., but class variables can also be manipulated without creating an instance of the class.

Làm thế nào để bạn gán một biến lớp trong Python?

Tạo các biến lớp Một biến lớp được khai báo bên trong lớp, nhưng ngoài bất kỳ phương thức thể hiện nào hoặc phương thức __init __ (). Theo quy ước, thông thường nó được đặt ngay bên dưới tiêu đề lớp và trước phương thức cấu trúc và các phương thức khác.A class variable is declared inside of class, but outside of any instance method or __init__() method. By convention, typically it is placed right below the class header and before the constructor method and other methods.

Một biến lớp trong Python là gì?

Biến lớp - một biến được chia sẻ bởi tất cả các trường hợp của một lớp.Các biến lớp được xác định trong một lớp nhưng ngoài bất kỳ phương thức nào của lớp.Các biến lớp không được sử dụng thường xuyên như các biến thể hiện.A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.

Làm thế nào để bạn truy cập một biến lớp trong Python?

Các biến được xác định bên trong lớp nhưng bên ngoài phương thức có thể được truy cập trong lớp (tất cả các phương thức bao gồm) bằng cách sử dụng thể hiện của một lớp.Ví dụ - bản thân.var_name.Nếu bạn muốn sử dụng biến đó ngay cả bên ngoài lớp, bạn phải khai báo biến đó là toàn cầu.using the instance of a class. For Example – self. var_name. If you want to use that variable even outside the class, you must declared that variable as a global.