Are static class variables possible in Python

0 votes

I'm looking for a string.contains or string.indexof method in Python.

I want to do:

if not somestring.contains("blah"):
   continue
Nov 30, 2020 in Python by Roshni
• 10,430 points
68 views

1 answer to this question.

0 votes

Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass:
...     i = 3
...
>>> MyClass.i
3 
  

If I point out, this creates a class-level I variable, but this is distinct from any instance-level i variable, so you could have

>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)

This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance.

answered Nov 30, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
1 answer

How can I prevent or alter access to class variables in Python?

The ActiveState solution that Pynt references makes instances of ...READ MORE

answered Dec 5, 2018 in Python by aryya
• 7,440 points
484 views
0 votes
1 answer

What are the naming conventions for variables and data types in python?

There are certain rules that we have ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,230 points
546 views
0 votes
0 answers

How are derived classes created dynamically from a base class in python?

Can you give an example? READ MORE

May 24, 2019 in Python by Waseem
• 4,540 points

edited May 27, 2019 by Omkar 73 views
0 votes
1 answer

What are first class objects in Python?

First-class objects in a language are handled uniformly ...READ MORE

answered Aug 2, 2019 in Python by Arvind
• 3,000 points
1,146 views
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,532 views
0 votes
1 answer

Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
158 views
0 votes
4 answers

How to print objects of class using print function in Python?

>>> class Test: ... ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,430 points
47,456 views
0 votes
1 answer

How are Python metaclasses different from regular class inheritance?

Python 3 class MyClass(object): = New-style class class MyClass: = New-style ...READ MORE

answered Nov 20, 2020 in Python by Gitika
• 65,870 points
55 views