How do I initialize a class class in Python?

Source: Internet
Author: User

1# A-1class definitions for FP trees2 classTreeNode:3 def _init_ (self,namevalue,numoccur,parentnode):4Self.name=Namevalue; # node's name5Self.count=Numoccur; # Number of occurrences6self.nodelink=None; # links to similar element items7Self.parent=parentnode; # parent Node8self.children={}; # child Nodes9 def Inc (Self,numcoour): # Method Inc () adds a given value to the count variableTenself.count+=numoccur; OneDef disp (self,ind=1): # Method Disp () to display the tree as text APrint' '*ind,self.name,' ', self.count); -          forChildinchself.children.values (): -Child.disp (ind+1);

The above defines a class TreeNode method with _init_ (Self,namevalue,numoccur,parentnode), Inc. (Self,numcoour), disp (self,ind=1);

Initialize error mode:

1>>>2>>>Importfpgrowth;3>>> Rootnode=treenode ('Pyramid', 9, None);4 Traceback (most recent):5File"<pyshell#5>", Line 1,inch<module>6Rootnode=fpgrowth.treenode ('Pyramid', 9, None);7Typeerror:object () takes no parameters

The right way:

1 2 Import fpgrowth; 3 >>> Os=treenode (); 4 >>> rootnode=os._init_ (' pyramid ', 9,none) 5 >>> rootnode6 >>> type (rootnode)7 <class  'nonetype'>8

How do I initialize a class class in Python?

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.