Python implements bitset classes similar to C ++
Bitset in C ++ and BitSet in Java are both very convenient and powerful in-place operations, which can greatly save memory and improve operation efficiency. Unfortunately, Python does not provide similar classes or modules. However, it is very easy to use the powerful ca
are like importedIf the class is like a module, then there should also be an import-like concept. In a class, this concept is called instantiation, and when a class is instantiated, it gets the object of a class.Create a class;Class MyStuff (object):def __init__ (self):SELF.A = ' How is it? 'def apple (self):Print ("I am classy apple")b = MyStuff () #类的实例化B.apple () #调用类的方法Print b.a# properties of the printing classAnalyzing the instantiation of a class this sentence actually
all standard exception classes for Python:
Exception name
Description
Baseexception
base class for all exceptions
Systemexit
Interpreter Request exited
Keyboardinterrupt
User interrupt execution (usually input ^c)
Exception
base class for general errors
Stopiteration
There are no more values for
code is posted and you're going to play it yourself.1 classclassmate ():2 def __init__(self):3Self.name =name4 5 defEat (self):6 Print '%s is eating.'%Self.name7 8 defDrink (self):9 Print '%sis drinking'%Self.nameTen One A classPythoner (classmate): - - defoccupation (self): the Print '%s is a pythoner.'%Self.name - - classAccounting (classmate): - + defoccupation (self): - Print '%s is a accounting.'%Self.name + AEva = Pythoner ('Ev
Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different for each object.
Still ta
The concept of classes in Python programming can be compared to the description of a certain type set. For example, "human" can be considered as a class, then we use the human class to define every specific person-you, me, and others as their objects. The class also has attributes and functions. attributes are some characteristics of the class, such as human attributes such as name, height, and weight. The
Some basic concepts of Python classesEverything in Python, the process of declaring variable functions, is actually a connection to the space allocated to the object in memory. Python, like Java, C + +, has its own properties and methods.A variable that belongs to an object or class is called a domain. There are two types of domains--objects that belong to each i
I. Definition and use of classes
The basic syntax for Python to define a class is:
Copy Code code as follows:
Class ClassName ([base class one, base class two ...]):
[Def __init__ (self, [agv1,agv2 ...]):] # define Constructordef method1 (self, [agv1,agv2 ...]): # member function
When used:
object instance name = Class name (argument list)
object instance name. member functio
Using static variables in python classes and functions
This example describes how to use static variables in python classes and functions. Share it with you for your reference. The specific analysis is as follows:
It seems impossible to use static variables in python
Concepts of classes and objectsClass is the most important concept of object-oriented design, which is the combination of feature and skill, while class is a combination of features and skills with similar objects.
In the real world: there must be objects first, then there are classes
In the program: Be sure to define the class first, then produce the object
在
dictionary, consisting of the data properties of the Class)__DOC__: Document string for Class__MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)__BASES__: All parent classes of a class make up elements that contain a tuple of all the parent classesThis article is from the "Note space" blog, so be sure to keep
We are familiar with the basic concepts of objects and classes. We will expand further so that we can actually use objects and classes.
Calling other information for a class
As mentioned in the previous lecture, when you define a method, you must have the parameter self. This parameter represents an object. Object has all the properties of a class, then we can invoke the class attribute through self.
The most important concept of object-oriented is class and instance (Instance), you must keep in mind that classes are abstract templates, such as student classes, while instances are specific "objects" created from classes, each with the same method, but their data may be different.
Still taking the student class as an example, in
There is no switch syntax in Python, and I want to use a switch-like feature in my practice, and search for the relevant content knowing that using a dictionary can do the steps I want. So, start.I'm using Python3, and I'm practicing using the Tkinter module to write a little game: Table tennis. In the test phase, I first typed: from Import *This loads the Tkinter module and uses * to make a little bit of code in the subsequent code input. After that,
"red"
Input in python terminal
>>> from CallDemo import Apple>>> help(Apple)Help on class Apple in module CallDemo:class Apple(__builtin__.object)| This is an Apple Class| | Methods defined here:| | get_color(self)| Get the Color of Apple.| get_color(self) -> str| | ----------------------------------------------------------------------| Data descriptors defined here:| | __dict__| dictionary for instance variables (if defined)| | __weakref__| lis
):
self. Name = name
self. Age = Age
self. Sex = Sex
def speak (self):
print "My name" + self. Name
def __str__ (self):
msg= ' My name is: ' +self. Name+ "," + "My Age is:" + self. Age + ', ' + ' I sex is: ' +self. Sex
# msg= ' My name is: ' +self. Name+ "," + "My Age is:" + str (self. Age) + ', ' + ' I sex is: ' +self. Sex return
msg
shanghai=peo (' Shanghai ', ' the ', ' Man ')
# SHANGHAI=PEO (' Shanghai ', ', ') '
msg= ' My name is: ' +self. Name+ "," + "My Age
different versions of the Python interpreter might change the __name to a different variable nameclassStudent (object):def __init__(Self,name,score): Self.__name=name self.__score=scoredefPrint_score (self):Print("%s:%s"% (self.__name, self.__score)) defSet_score (self, score):#by internal methods to modify the __score #在方法中 inside the class, you can check the parameters to avoid passing invalid arguments: if0 : Self.__score=scoreElse:
1Python 2inheritance in. 72In Python 2. 7, the inheritance syntax is slightly different, and the definition of the Electriccar class is similar to the following:3 classCar (object):4 def __init__(self, make, model,year):5--snip--6 7 classElectriccar (Car):8 def __init__(self, make, model,year):9 Super (Electriccar, self). __init__ (make, model, year) Ten--snip-- OneThe function super () requires two arguments: a subclass name and an ob
on the screen{' name ': ' James Lee ', ' Date of birth ': ' 2002-3-14 ', ' score ': [' 3.21 ', ' 3.10 ', ' 3.01 ']}(3) Main program codeTdThe_james2 = td.chdict (' F:\Python\Python file \james2.txt ')Print (The_james2)(4) The_dict module codedefSanitize (TIME_STR):#Pass in the string and change '-' and ': ' to '. ' and return, or return directlyif'-'inchTIME_STR:(x, y) = Time_str.split ('-', 1)return(x+"."
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.