python learning notes 1

Keywords: Python Pycharm

This article is mainly aimed at sorting out some knowledge points of learning python in these two days

1.os related parameter learning

Use of common sub functions of os Library

os.sep: replaces OS specific path delimiters

os.name: indicates the working platform you are using. For example, it is' nt 'for Windows and' posix 'for Linux/Unix users.

os.getcwd: get the current working directory, that is, the directory path of the current python script.

os.getenv() and os.putenv: used to read and set environment variables respectively

os.listdir(): returns all file and directory names in the specified directory

os.remove(file): deletes a file

os.stat (file): get file properties

os.chmod(file): modify file permissions and timestamp

os.mkdir(name): create directory

os.rmdir(name): delete directory

OS. Removediers (r "c: \ python"): delete multiple directories

os.system(): run shell command

os.exit(): terminates the current process

os.linesep: gives the line terminator of the current platform. For example, Windows uses' \ r\n ', Linux uses' \ n' and Mac uses' \ r '

os.path.split(): returns the directory name and file name of a path

os.path.isfile() and os.path.isdir() respectively verify whether the given path is a directory or a file

OS. Path. Exist(): verify whether the given path really exists

os.listdir(dirname): lists the directories and files under dirname

os.getcwd(): get the current working directory

os.curdir: returns the current directory ('.')

os.chdir(dirname): change the working directory to dirname

os.path.isdir(name): judge whether name is a directory. If it is not a directory, false will be returned

os.path.dirname(name): returns the directory where the file is located

os.path.isfile(name): judge whether the file name exists. If it does not exist, false is returned

os.path.exists(name): judge whether there is a file or directory name

os.path.getsize(name): or the file size. If name is a directory, 0L is returned

os.path.abspath(name): get absolute path

os.path.isabs(): judge whether it is an absolute path

os.path.normpath(path): Specifies the path string form

os.path.split(name): split the file name and directory (in fact, if you use the directory completely, it will separate the last directory as the file name, and it will not judge whether the file or directory exists)

OS. Path. Splittext(): separate file name and extension

os.path.join(path,name): connect the directory with the file name or directory

os.path.basename(path): returns the file name

os.path.dirname(path): returns the file path

import os
def path1(file1,match1=os.sep):
	
	# abspath1=(os.path.abspath(__file__))
	dir1=os.path.dirname(file1)
	if dir1.endswith(os.sep) is True:
		return dir1
	else:
		return dir1+os.sep

abspath1=(os.path.abspath(__file__))
print(path1(abspath1))

2. Learning about class inheritance

Include the learning of raise NotImplementedError in class functions

class animal(object):
	def __init__(self,name,age):
		self.name=name
		self.age=age

	def call(self):

		print(self.name+'is saying Hello')

	def sit(self):

		raise NotImplementedError


class cat(animal):

	def __init__(self,name,age,sex):
		super(cat,self).__init__(name,age)
		self.sex=sex

	def sit(self):
		print(self.name+' is sitting down')


# a=animal('dog','25')
# a.call()
# a.sit()

b=cat('dog','25','male')
b.call()
b.sit()

The program defines two classes, the animal base class, cat inherits the animal class, where super (cat, self)__ init__ (name, age) is a variable used to inherit the animal class

When the inherited class sub function sit is not redefined, the raise NotImplementedError error will be fired

Traceback (most recent call last):
  File "C:\Users\weijiangbin\Desktop\python_learning\test20210503.py", line 78, in <module>
    b.sit()
  File "C:\Users\weijiangbin\Desktop\python_learning\test20210503.py", line 59, in sit
    raise NotImplementedError
NotImplementedError

Only if the function is defined, there will be no error. You can try it

3. Define the types of input parameters and output parameters in the function

def add1(a:int,b:int)->int:
	return a+b

c=1
d=3
print(add1(c,d))

As mentioned above, add1 functions a and b are forcibly defined as int type, and the output is defined as int type output with - > int

4.isinstance replaces type to define type comparison

  • type() does not consider a subclass as a parent type and does not consider inheritance.

  • isinstance() will consider the subclass as a parent type and consider the inheritance relationship.

class animal(object):
	def __init__(self,name,age):
		self.name=name
		self.age=age

	def call(self):

		print(self.name+'is saying Hello')

	def sit(self):

		raise NotImplementedError


class cat(animal):

	def __init__(self,name,age,sex):
		super(cat,self).__init__(name,age)
		self.sex=sex

	def sit(self):
		print(self.name+' is sitting down')


a=animal('dog','25')
a.call()
# a.sit()

b=cat('dog','25','male')
b.call()
b.sit()

print(isinstance(a,animal))
print(type(a)==animal)
print(isinstance(b,animal))
print(type(b)==animal)

The result is

Posted by JD^ on Sat, 11 Sep 2021 11:54:02 -0700